2

我必须制作一个程序,从用户那里获取一个短语,然后将他们输入的短语的加密代码返回给用户。我们将使用数组或枚举列表来保存数据。我使用 ROT13 进行编码(用字母表前后的 13 个位置替换每个英文字母)。我的程序运行,但它只允许我输入一个短语,然后说:

java.lang.ArrayIndexOutOfBoundsException: 26
    at J4_1_EncryptionVer2.main(J4_1_EncryptionVer2.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

我不确定我的程序有什么问题!请帮忙!谢谢

        import java.io.*;

public class J4_1_EncryptionVer2
{
  public static void main (String [] args) throws IOException
  {
    BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));

   String letterA [] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
   String letterB [] = {"N","O","P","Q","R","S","T","U","V","W","X","Y","Z","A","B","C","D","E","F","G","H","I","J","K","L","M"};

    System.out.println ("Please enter a phrase: ");
    String message = myInput.readLine();

    int x = 0; 
    while (x < message.length()){

      String text = message;
      String letter = Character.toString(text.charAt(x));
        x++;

      int i = 0;

     if(letter.equals (letterA[i])){
        System.out.println (letterB[i]);
      }
      while (!(letter.equals (letterA[i]))){
        i++;
        if(letter.equals (letterA[i])){
        System.out.println (letterB[i]);
      }

    }
  }
}
}
4

5 回答 5

1

三个问题:

  1. while (x <= message.length())应该while (x < message.length())
  2. x++应该在之后移动text.charAt(x)
  3. 用于equals比较字符串值。

当然你只能用AZ来测试

于 2013-09-13T04:06:59.923 回答
0

对于初学者来说,异常的原因是数组索引超出了第 28 行的界限。您不必费心检查 i 是否在界限内。此外,您正在从用户输入的消息中获取一个字母,它可以是键盘上的任何内容,然后在 letterA 中查找它,它只有 26 个大写字母!基本上,您从 letterA 的末尾走开,永远找不到字母。

于 2013-09-13T04:00:50.610 回答
0

您仅为大写字母字符提供letterA []。你的比较循环有些好。但是你必须检查你的 while (letter != letterA[i])循环。

确保将输入作为大写字母字符仅用于测试输出。

于 2013-09-13T04:23:00.070 回答
0
import java.io.*;

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {
        BufferedReader myInput = new BufferedReader(new InputStreamReader(System.in));

        char letterA[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
        char letterB[] = {'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'};

        System.out.println("Please enter a phrase: ");
        String message = myInput.readLine();
        message = message.toUpperCase();
        char letterC[] = new char[message.length()]; 

        int x = 0;
        for (int j = 0; j < message.length(); j++) {
            for (int i = 0; i < letterA.length; i++){
                if(message.charAt(j)==letterA[i]){
                    letterC[j]=letterB[i];
                }
            }

        }

        for (int j = 0; j < message.length(); j++) {
            System.out.print(letterC[j]);
        }
        System.out.println("  -- done");
    }
}
于 2013-09-13T04:27:17.453 回答
0

就像上面其他用户所建议的那样,您没有注意数组大小..并且没有什么可以阻止您的 while 循环:

while (!(letter.equals (letterA[i])))

在处理数组时,我不太喜欢 while。在我看来,这就是你的外部时间应该是什么样子。

      while (x < message.length())          
            {
                String text = message;
                String letter = Character.toString(text.charAt(x));
                x++;

                for(int i=0; i<letterA.length; i++)
                {
                    if(letter.equals(letterA[i]))if(letter.equals(letterA[i]))
                    {
                        System.out.print (letterB[i]);
                        break;
                    }
                    else if (letter.equals(" "))
                    {
                        System.out.print(" ");
                        break;
                    }
                }
            }

我在我的机器上测试了它,它工作得很好。

输入:这是我的明文

输出:GUVF VF ZL CYNVAGRKG

于 2013-09-13T04:45:41.597 回答