2016-12-30 166 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'中。那有意義嗎? –