Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Caesar密码包括大小写字符_Java - Fatal编程技术网

Java Caesar密码包括大小写字符

Java Caesar密码包括大小写字符,java,Java,我是Java新手,我尝试编写Caesar密码,包括大写和小写字符。我不知道为什么会出现编码错误。有人能帮我吗,我非常感谢。 这是我目前掌握的代码 public class CaesarCipher{ char[] encoder = new char[52]; char[] decoder = new char[52]; public CaesarCipher(int rotation) { for(int k=0 ; k < 2

我是Java新手,我尝试编写Caesar密码,包括大写和小写字符。我不知道为什么会出现编码错误。有人能帮我吗,我非常感谢。 这是我目前掌握的代码

public class CaesarCipher{

    char[] encoder = new char[52]; 
    char[] decoder = new char[52]; 

     public CaesarCipher(int rotation) 
     {
       for(int k=0 ; k < 26 ; k++)
       {
           encoder[k] = (char) ('A' + (k + rotation) % 26);
           decoder[k] = (char) ('A' + (k - rotation + 26) % 26); 
       }
       for(int j = 26 ; j < 52 ; j++ )
       {
           encoder[j] = (char)('a' + (j + rotation) % 26); 
           decoder[j] = (char)('a' + (j - rotation) % 26); 
       }
     }

     public String encrypt(String message) { 
         char[] msg = message.toCharArray();
         for(int i = 0 ; i < msg.length ; i++){
            if(Character.isUpperCase(msg[i])){
                msg[i] = encoder[msg[i] - 'A'];
            }   
            else{
                int n = msg[i] - 'a' ;
                msg[i] = encoder[26 + n];
            }
         }
         return new String(msg); 
     }

     public String decrypt(String secret) { 
        char[] msg = secret.toCharArray();
         for(int i = 0 ; i < msg.length ; i++){
            if(Character.isUpperCase(msg[i])){
                msg[i] = decoder[msg[i] - 'A'];
            }   
            else{
                 int n = msg[i] - 'a';
                msg[i] = decoder[26 + n];
            }
         }
         return new String(msg); 
     }


  public static void main(String[] args) {
    CaesarCipher cipher = new CaesarCipher(3);
    String message = "There Is an APPle";
    String coded = cipher.encrypt(message);
    System.out.println("Secret:  " + coded);
    String answer = cipher.decrypt(coded);
    System.out.println("Message: " + answer);         
  }
}
公共类密码{
char[]编码器=新字符[52];
字符[]解码器=新字符[52];
公共密码(整数轮换)
{
对于(int k=0;k<26;k++)
{
编码器[k]=(字符)('A'+(k+旋转)%26);
解码器[k]=(字符)('A'+(k-旋转+26)%26);
}
对于(int j=26;j<52;j++)
{
编码器[j]=(字符)('a'+(j+旋转)%26);
解码器[j]=(字符)('a'+(j-旋转)%26);
}
}
公共字符串加密(字符串消息){
char[]msg=message.toCharArray();
对于(int i=0;i
运行我得到的代码

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -39
  at t1.CaesarCipher.encrypt(CaesarCipher.java:30)
  at t1.CaesarCipher.main(CaesarCipher.java:54)
此时,正在执行的代码是

            int n = msg[i] - 'a' ;
            msg[i] = encoder[26 + n];
我是5,但你的输入数据是“有一个苹果”


索引5处的字符是一个空格,但您试图从中减去“a”,得到一个负索引。

您得到的错误是什么?