0

下面你会发现我使用 toCharArray 将字符串发送到数组。然后我使用 for 语句移动字母的值...

for(i = 0; i < letter.length; i++){
               letter[i] += (shiftCode);
               System.out.print(letter[i]);
            }

但是,当我使用 shiftCode 移动值时,例如...

a 移位 -1;我得到一个符号@。有没有办法将字符串发送到 shiftCode 或告诉 shiftCode 只使用字母?我需要它来查看我的文本,比如“aaron”,当我使用 for 语句时,只遍历 az 并忽略所有符号和数字。我认为这很简单...

letter=codeWord.toCharArray(a,z);

但是尝试不同的形式并在谷歌上搜索它并没有给我任何结果。也许它与正则表达式或其他东西有关?您将在下面找到我的程序的完整副本;它完全按照我想要的方式工作;但它会遍历字母和符号。我还尝试在线查找 toCharArray 的说明,但如果存在任何参数,我无法找到它们。

我的程序...

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/*
 * Aaron L. Jones
 * CS219
 * AaronJonesProg3
 * 
 * This program is designed to -
 * Work as a Ceasar Cipher
 */

/**
 *
 * Aaron Jones
 */
public class AaronJonesProg3 {
    static String codeWord;
    static int shiftCode;
    static int i;
    static char[] letter;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // Instantiating that Buffer Class
        // We are going to use this to read data from the user; in buffer
        // For performance related reasons
        BufferedReader reader;

        // Building the reader variable here
        // Just a basic input buffer (Holds things for us)
        reader = new BufferedReader(new InputStreamReader(System.in));

        // Java speaks to us here / We get it to query our user
        System.out.print("Please enter text to encrypt: ");

        // Try to get their input here
        try {    
            // Get their codeword using the reader
            codeWord = reader.readLine();

            // Make that input upper case
            codeWord = codeWord.toUpperCase();
            // Cut the white space out
            codeWord = codeWord.replaceAll("\\s","");
            // Make it all a character array
            letter = codeWord.toCharArray();
        }
        // If they messed up the input we let them know here and end the prog.
        catch(Throwable t) {
            System.out.println(t.toString());
            System.out.println("You broke it. But you impressed me because"
                    + "I don't know how you did it!");
        }

        // Java Speaks / Lets get their desired shift value
        System.out.print("Please enter the shift value: ");

        // Try for their input
        try {
               // We get their number here
               shiftCode = Integer.parseInt(reader.readLine());
        }
        // Again; if the user broke it. We let them know.
        catch(java.lang.NumberFormatException ioe) {
            System.out.println(ioe.toString());
            System.out.println("How did you break this? Use a number next time!");
        }

        for(i = 0; i < letter.length; i++){
           letter[i] += (shiftCode);
           System.out.print(letter[i]);
        }

        System.out.println();

        /****************************************************************
         ****************************************************************
         ***************************************************************/
        // Java speaks to us here / We get it to query our user
        System.out.print("Please enter text to decrypt: ");

        // Try to get their input here
        try {    
            // Get their codeword using the reader
            codeWord = reader.readLine();

            // Make that input upper case
            codeWord = codeWord.toUpperCase();
            // Cut the white space out
            codeWord = codeWord.replaceAll("\\s","");
            // Make it all a character array
            letter = codeWord.toCharArray();
        }
        // If they messed up the input we let them know here and end the prog.
        catch(Throwable t) {
            System.out.println(t.toString());
            System.out.println("You broke it. But you impressed me because"
                    + "I don't know how you did it!");
        }

        // Java Speaks / Lets get their desired shift value
        System.out.print("Please enter the shift value: ");

        // Try for their input
        try {
               // We get their number here
               shiftCode = Integer.parseInt(reader.readLine());
        }
        // Again; if the user broke it. We let them know.
        catch(java.lang.NumberFormatException ioe) {
            System.out.println(ioe.toString());
            System.out.println("How did you break this? Use a number next time!");
        }

        for(i = 0; i < letter.length; i++){
           letter[i] += (shiftCode);
           System.out.print(letter[i]);
        }

        System.out.println();
    }
}
4

3 回答 3

2

用于Character.isLetter()分离出字母......

看到这个链接......

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Character.html

例如:

public class Test{
   public static void main(String args[]){
      System.out.println( Character.isLetter('c'));
      System.out.println( Character.isLetter('5'));
   }
}

输出:

真假

于 2012-09-08T17:48:40.270 回答
1

首先用 . 检查数组的每个字符if(Character.isLetter(letter[i]))。然后使用您的移位代码,检查字母[i] 处的字符与相应的最后一个字母字符(即大写或小写'z')之间的区别。有关详细说明,请参阅我在此论坛中此链接上对凯撒密码的回答。

于 2012-09-08T17:54:40.257 回答
0
        // iterating
    for(i = 0; i < letter.length; i++){
        letter[i] += (shiftCode); // Start adding shiftcode to letter
        if(!(letter[i] <= 'Z')) {
            // if its bigger than Z subtract 26
            letter[i] -= (26);
        }
        // less than A?
        if(!(letter[i] >= 'A')) {
            // add 26
            letter[i] += (26);
        }
        System.out.print(letter[i]); // print it all out
    }

上面的代码是我的问题的正确答案。我发现 UNICODE 是有编号的,如果你不疯狂地选择一个大的数字,通过使用数字 26,我们可以很好地移动。我认为明智的做法是编写一个强制用户最多迭代-26到26的catch。

于 2012-09-09T05:34:15.837 回答