Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.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
ceaser密码加密/解密javascript给定字符串索引错误_Javascript_Java - Fatal编程技术网

ceaser密码加密/解密javascript给定字符串索引错误

ceaser密码加密/解密javascript给定字符串索引错误,javascript,java,Javascript,Java,我被指派在Jgrasp中创建一个Java程序,该程序使用凯瑟密码对消息进行加密和解密。我们的老师给了我们一个shell和示例,所有这些都是我用来创建代码的。问题是代码编译得很好,但是当试图运行它时,我得到了一个Sting索引错误。我有点理解这个错误的意思,但我不知道如何解决它。如果有人能向我解释是什么导致了错误以及如何解决它,我们将不胜感激 更新:感谢Spectric指出我的for循环中的错误!我现在的问题是,即使我使用了字符串message=message.toUpperCase();,我似乎

我被指派在Jgrasp中创建一个Java程序,该程序使用凯瑟密码对消息进行加密和解密。我们的老师给了我们一个shell和示例,所有这些都是我用来创建代码的。问题是代码编译得很好,但是当试图运行它时,我得到了一个Sting索引错误。我有点理解这个错误的意思,但我不知道如何解决它。如果有人能向我解释是什么导致了错误以及如何解决它,我们将不胜感激

更新:感谢Spectric指出我的for循环中的错误!我现在的问题是,即使我使用了字符串message=message.toUpperCase();,我似乎也无法在所有大写字母中输出消息。我还添加了输出行,当加密消息显示时,解密消息只是空白。最后,当向消息中添加空格时,我收到一个错误,在进行加密/解密时,如何解释非字符输入?我假设它是某种形式的if语句,但我一直无法破解它。谢谢你的帮助

代码:

    import java.util.*;
    
    public class CaesarCipherShell {
       public static void main(String[] args) 
       {
          Scanner kb = new Scanner(System.in);
          run(kb);
              
       }
       public static void run(Scanner kb)
       {
         System.out.println("How many times would you like to use the app? ");
          int count = kb.nextInt();
         for(int i = 1; i <= count; i++)
         {
          System.out.println("Please enter a message: ");
           String message = kb.next();
          System.out.print("Please enter a key: ");
           int key = kb.nextInt();
          encrypt(message, key);
          decrypt(message, key);
         }
       }
       public static String encrypt(String message, int key) 
       {
          String MESSAGE = message.toUpperCase();
          String result = "";
          for(int i = 0; i < MESSAGE.length(); i++) 
          {
             char c = MESSAGE.charAt(i);
             if(c >= 'A' && c <= 'Z')
             {
                c = (char)(c + key);
                if(c > 'Z')
                {
                   c = (char)(c - 26);
                }
                else
                {
                   c = (char)(c + 26);
                }
             }
          result = c + result;     
          }
       System.out.println("Encrypted message:" + result);
       return result;
       }
       public static String decrypt(String MESSAGE, int key) 
       {
          String result = "";
          for(int i = 0; i < MESSAGE.length(); i++)
          {
             char c = MESSAGE.charAt(i);
             if(c >= 'A' && c <= 'Z')
             {
                c = (char)(c - key);
                if(c < 'A')
                {
                   int diff = 'A' - c;
                   c = (char)('Z' - diff + 1);
                }
                else if (c > 'Z')
                {
                   int diff = 'Z' - c;
                   c = (char)('A' + diff + 1);
                }
             result = c + result;
             }                          
          
          }
       System.out.println ("Decrypted message:" +result);
       return result;   
       }
    }

代码中负责遍历字符串中每个字符的
for
循环已中断

for(int i = 0; i <= MESSAGE.length(); i++) 

在for循环中,您的索引转到message.length,它大于最大索引message.length-1,因为索引从零开始。您可以通过使比较严格低于而不是低于或等于来修复它。对于键,您必须使用
int key=Integer.parseInt(kb.nextLine())因为它假设您在键入“我喜欢海龟”之后键入了数字。非常感谢!!!完全错过了我的for循环中的=符号。现在一切都很好,但我现在确实有几个小问题。首先是message.toUpperCase();似乎没有将消息转换为我输出中的所有大写字母。第二,当我获取要输出的加密消息时,解密消息只输出空白。另外,如何解释消息中的非字符输入,如空格?我在帖子中更新了代码,并在新的输出中添加了代码。谢谢你的帮助@小混混我来看看
for(int i = 0; i <= MESSAGE.length(); i++) 
public class CaesarCipherShell {
   public static void main(String[] args) 
   {
      Scanner kb = new Scanner(System.in);
      run(kb);
          
   }
   public static void run(Scanner kb)
   {
     System.out.println("How many times would you like to use the app? ");
      int count = kb.nextInt();
     for(int i = 1; i <= count; i++)
     {
      System.out.println("Please enter a message: ");
       String message = kb.next();
      System.out.print("Please enter a key: ");
       int key = kb.nextInt();
      encrypt(message, key);
      decrypt(message, key);
     }
   }
   public static String encrypt(String message, int key) 
   {
      String MESSAGE = message.toUpperCase();
      String result = "";
      for(int i = 0; i < MESSAGE.length(); i++) 
      {
         char c = MESSAGE.charAt(i);
         if(c >= 'A' && c <= 'Z')
         {
            c = (char)(c + key);
            if(c > 'Z')
            {
               c = (char)(c - 26);
            }
            else
            {
               c = (char)(c + 26);
            }
         }
      result = c + result;     
      }
   return result;
   }
   public static String decrypt(String MESSAGE, int key) 
   {
      String result = "";
      for(int i = 0; i < MESSAGE.length(); i++)
      {
         char c = MESSAGE.charAt(i);
         if(c >= 'A' && c <= 'Z')
         {
            c = (char)(c - key);
            if(c < 'A')
            {
               int diff = 'A' - c;
               c = (char)('Z' - diff + 1);
            }
            else if (c > 'Z')
            {
               int diff = 'Z' - c;
               c = (char)('A' + diff + 1);
            }
         result = c + result;
         }                          
      
      }
   return result;   
   }
}