2016-12-30 61 views
0

我是新来的Java,这是我必须做的:
编写一个程序,让用户输入消息和移位值,然后输出编码的消息
编写一个单独的程序,允许用户使用第一个程序输入编码消息,然后为您解码,并且不能使用StringBuffer或StringBuilder凯撒密码的挑战:编码和解码代码

我在这里要做的是制作程序的第二部分,但我有一些问题。当你添加移位值时,无论输入什么数字,当我尝试解码时,它都会将它编码为1,这会给我一个错误。

public class part2 { 
    public static void main(String[] args) { 
     Scanner sc = new Scanner(System.in); 
     String text; 
     String key; 
     int shift; 

     System.out.println("Enter message:"); 
     text = sc.nextLine(); 
     System.out.println("Enter encryption key:"); 
     key = sc.next(); 
     shift = key.length(); { 
      System.out.println("1.Encrypt\n2.Decrypt\n3.Exit..."); 
      int choice = sc.nextInt(); 
      switch (choice) { 
       case 1: 
        System.out.println("Encryptedmessage..." + encrypt(text, shift)); 
        break; 
       case 2: 
        //send retrived string from encrypt() method and keyLength to decrypt() method it returns 'Decrypted' string 
        System.out.println("Decrypted message..." + decrypt(encrypt(text, shift), shift)); 
        break; 
       case 3: 
        //exit from the program 
        System.exit(0); 
        break; 
       default: 
        System.out.println("Invalid option.."); 
      } 
     } 
    } 
    public static String encrypt(String text, int shift) { 

     for (int i = 0; i < text.length(); i++) { 
      char letter = text.charAt(i); 

      // shift only letters (leave other characters alone) 
      if (letter >= 'a' && letter <= 'z') { 
       letter = (char)(letter + shift); 

       // may need to wrap around 
       if (letter > 'z') { 
        letter = (char)(letter - 26); 
       } else if (letter < 'a') { 
        letter = (char)(letter + 26); 
       } 
      } 
      System.out.print(letter); 
     } 
     return null; 
    } 

    public static String decrypt(String str, int keyLength) { 
     String decrypted = ""; 
     for (int i = 0; i < str.length(); i++) { 

      int c = str.charAt(i); 

      if (Character.isUpperCase(c)) { 
       c = c - (keyLength % 26); 
       if (c < 'A') 
        c = c + 26; 
      } else if (Character.isLowerCase(c)) { 
       c = c - (keyLength % 26); 
       if (c < 'a') 
        c = c + 26; 
      } 
      decrypted = decrypted + (char) c; 
     } 
     return decrypted; 
    } 
} 

这是我的第一部分代码:

public static void main(String[] args) { 

     Scanner console = new Scanner(System.in); 
     System.out.print("Type a message you want to be coded: "); 
     String message = console.nextLine(); 
     message = message.toLowerCase(); 

     System.out.print("Enter a Shift: "); 
     int key = console.nextInt(); 
    if (key < 1 || key > 25) { 
       System.out.printf(" The key must be between 1 and 25, you entered %d.\n", key); 
      } 
     while (key < 1 || key > 25); 

     encode(message, key); 
    } 

    // This method encodes the given text string using a Caesar 
    // cipher, shifting each letter by the given number of places. 
    public static void encode(String text, int shift) { 
     System.out.print("The encoded message: \n"); 
     for (int i = 0; i < text.length(); i++) { 
      char letter = text.charAt(i); 

      // shift only letters (leave other characters alone) 
      if (letter >= 'a' && letter <= 'z') { 
       letter = (char) (letter + shift); 

       // may need to wrap around 
       if (letter > 'z') { 
        letter = (char) (letter - 26); 
       } else if (letter < 'a') { 
        letter = (char) (letter + 26); 
       } 
      } 
      System.out.print(letter); 
     } 
    } 

这是第一部分的代码和它完美的作品。只需要第二部分的帮助

回答

0

这里有几个问题。

首先,你没有完成第一部分,就像你想的那样。让我们来看看你的encrypt方法:

public static String encrypt(String text, int shift) { 

    for (int i = 0; i < text.length(); i++) { 
     char letter = text.charAt(i); 

     // shift only letters (leave other characters alone) 
     if (letter >= 'a' && letter <= 'z') { 
      letter = (char)(letter + shift); 

      // may need to wrap around 
      if (letter > 'z') { 
       letter = (char)(letter - 26); 
      } else if (letter < 'a') { 
       letter = (char)(letter + 26); 
      } 
     } 
     System.out.print(letter); // <- This is part of your problem 
    } 
    return null; // <- This is part of your problem 
} 

当我们调用此,我们输出的编码字符串到控制台,但看看最后一行:我们回到null。您需要找出一种方法来存储编码的消息,然后将其返回。否则,你将永远得到这条线NullPointerExceptionmain方法:

System.out.println("Decrypted message..." + decrypt(encrypt(text, shift), shift)); 

第二个问题是,你可能会误解你的任务。看看上面的那一行,一旦你有encrypt工作,你仍然有一个没用的decrypt选项:它会一直给你回你输入的信息。我想认为你想要做的只是decrypt消息,与用户传递已经加密的消息。但是,那只是我的预感,你可能想和你的老师核对一下。

+0

@Jay - 好的,我看到你从part1中添加了你的代码。你是对的,看起来很好。我所得到的是,在第二部分中你的编码“加密”不太正确。看看我在我的回答中发出的线条。你可以通过在'encrypt'的顶部添加一行来解决这个问题,然后改变我评论过的那两行。您需要返回编码的消息,而不仅仅是打印它。 –

+0

我试着修复它,但如果我删除了返回语句,它给了我一个错误,并在您的评论中写道,我可以通过添加一行来解决此问题。你是什​​么意思?谢谢你的帮助 – Jay

+0

@Jay - 我真的很想帮助你理解这个问题,而不仅仅是为你做作业。我们来看看这个方法。签名是'public static String encrypt(String text,int shift)'。第三个字,'字符串'意味着你需要'返回'一个'字符串'。在这种情况下,'String'应该是加密的消息。现在,你只返回null。而不是仅仅打印到控制台,你需要将该消息存储到一个'String'变量和'return'中。那有意义吗? –