执行caesar密码时的JAVA索引问题

执行caesar密码时的JAVA索引问题,java,arrays,string,Java,Arrays,String,代码目标:(使用netbeans) 我试图编写这段代码来应用凯撒密码算法,其中可以按顺序使用多个密钥,例如:key=[1,2,3],text=test,其中它将在“t”上使用密钥作为“1”,然后在“e”上使用密钥“2”,然后在“s”上使用密钥“3”,然后在最后一个t上返回密钥“1” 输出: run: Enter the text : mohammad rahmat Enter the number of keys you want to use : 3 Enter 3 number of key

代码目标:(使用netbeans)

我试图编写这段代码来应用凯撒密码算法,其中可以按顺序使用多个密钥,例如:key=[1,2,3],text=test,其中它将在“t”上使用密钥作为“1”,然后在“e”上使用密钥“2”,然后在“s”上使用密钥“3”,然后在最后一个t上返回密钥“1”

输出:

run:
Enter the text : mohammad rahmat
Enter the number of keys you want to use : 3
Enter 3 number of keys : 1
2
3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at caesar.Caesar.main(Caesar.java:68)
nqkbopbfbsckncw
Java Result: 1
BUILD SUCCESSFUL (total time: 9 seconds)
代码:

package caesar;    

import java.util.Scanner;    

public class Caesar {

/**
 * @param args the command line arguments
 */
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
    // TODO code application logic here
    char table[] = {'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 tmp;
    System.out.print("Enter the text : ");
    tmp = input.nextLine();
    char text[] = tmp.toCharArray();
    System.out.print("Enter the number of keys you want to use : ");
    int keyNo = input.nextInt();
    int key[] = new int[keyNo];
    System.out.printf("Enter %d number of keys : ",keyNo);
    for (int i = 0; i < keyNo; ++i){
        key[i] = input.nextInt();
    }

    char entext[] = new char[text.length];
    int k = 0;
    int a = 0;
    int count = 0;
    while (k < text.length){
        int j = 0;
        while (text[a] != table[j])
            j++;
        if (key[count]+j >= table.length){
            entext[a] = table[(j+key[count])%table.length];
        }

        else entext[a] = table[j+key[count]];

        a++;
        count++;
        if (count == keyNo)
            count = 0;
        k++;


    }
    String answer = new String(entext);
    System.out.printf("ENCRYPTION : %s \n\n",answer);


    char detext[] = new char[text.length];
    k = 0;
    a = 0;
    count = 0;
    while (k < text.length){
        int j = 0;
        while (text[a] != table[j])
            j++;
        if (key[count]-j < 0){
            detext[a] = table[table.length+(key[count]-j)];
        }

        else detext[a] = table[j-key[count]];

        a++;
        count++;
        if (count == keyNo)
            count = 0;
        k++;


    }
    String answer2 = new String(detext);
    System.out.printf("DECRYPTION : %s\n\n",answer2);
}
}
packaesar;
导入java.util.Scanner;
公营恺撒{
/**
*@param指定命令行参数
*/
静态扫描仪输入=新扫描仪(System.in);
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
字符表[]={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’、‘’、‘‘‘’;
串tmp;
System.out.print(“输入文本:”);
tmp=input.nextLine();
char text[]=tmp.toCharArray();
System.out.print(“输入要使用的密钥数:”);
int keyNo=input.nextInt();
int key[]=新的int[keyNo];
System.out.printf(“输入%d个键:”,键号);
对于(int i=0;i=表长度){
entext[a]=表[(j+键[count])%table.length];
}
else entext[a]=表[j+键[count]];
a++;
计数++;
if(count==keyNo)
计数=0;
k++;
}
字符串应答=新字符串(entext);
System.out.printf(“加密:%s\n\n”,答案);
char detext[]=新字符[text.length];
k=0;
a=0;
计数=0;
while(kelse detext[a]=表[j键[count]];
a++;
计数++;
if(count==keyNo)
计数=0;
k++;
}
字符串应答2=新字符串(detext);
System.out.printf(“解密:%s\n\n”,应答2);
}
}

您应该使用调试器来分析代码

detext[a] = table[table.length+(key[count]-j)];

您正在递增
j
并从
count
中减去它,后者为零。在操作查找数组索引之前,递增
count

我最终找到了它,在将其转换回原始文本时,我必须获取转换后的文本,换句话说:

替换

while (k < text.length){
    int j = 0;
    while (text[a] != table[j])
while(k

while(k
用合适的IDE调试代码..你会知道原因的..@GirlyGirl我的问题在那里说了,我在构建中遇到了错误,我似乎无法找出为什么你没有在那里得到模式,它的键[count],它永远不能为零,并且有一个if语句,所以索引永远不会为负,至少我认为sodetext[a]=表[j键[计数];
while (k < entext.length){
        int j = 0;
        while (entext[a] != table[j])