我必须制作一个程序,从用户那里获取一个短语,然后向用户返回他们输入的短语的加密代码。我们使用数组或枚举列表来保存数据。我使用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]);
}
}
}
}
}
答案 0 :(得分:1)
三个问题:
while (x <= message.length())
应为while (x < message.length())
x++
应该在text.charAt(x)
equals
来比较字符串值。当然你只能用A-Z来测试
答案 1 :(得分:0)
对于初学者来说,异常的原因是数组索引超出界限第28行。你不必费心去检查我是否在界限范围内。此外,您正在从用户输入的消息中取一封信,这封信可以是键盘上的任何内容,然后在letterA中查找,这只是26个大写字母!基本上你走完了字母的结尾,从未找到信。
答案 2 :(得分:0)
您仅为大写字母字符提供 letterA [] 。你的比较循环是好的。但你必须检查 while(letter!= letterA [i])循环。
确保将程序输入作为大写字母字符仅用于测试输出。
答案 3 :(得分: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");
}
}
答案 4 :(得分:0)
像上面提到的其他用户一样,你没有处理数组大小..并且没有什么能阻止你的while循环:
while (!(letter.equals (letterA[i])))
在处理数组时我不太喜欢。在我看来,这就是你的外表应该是什么样子。
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;
}
}
}
我在我的机器上进行了测试,效果很好。
输入:这是我的PLAINTEXT
输出:GUVF VF ZL CYNVAGRKG