0

我的程序应该从用户那里获取一个短语,然后将他们输入的短语的加密代码(ROT13 或 ATBASH)返回给用户。我的代码编译所有内容并让用户输入所需的内容,但是当他们输入要加密的短语时,什么也没有发生……就像新的加密代码没有出现一样,我不知道它有什么问题!请帮忙!谢谢!

   import java.io.*;

public class J4_1_EncryptionVer4
{
  public static void main (String [] args) throws IOException
  {
    BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));//BufferedReader reads user input

    //String array letterA[] is initialized
    String [][] letterA = new String [][]{
    {"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"},
    {"N","O","P","Q","R","S","T","U","V","W","X","Y","Z","A","B","C","D","E","F","G","H","I","J","K","L","M"},
    {"Z","Y","X","W","V","U","T","S","R","Q","P","O","N","M","L","K","J","I","H","G","F","E","D","C","B","A"},
    };

    System.out.println ("Enter '1' for ROT13 or '2' for ATBASH");//asks user to choose method
    String numA = myInput.readLine();//reads user input and assigns it to string
    int num = Integer.parseInt (numA);//converts string to integer
    int a = 0;//int a is declared

    if (num == 1){//if user enters 1
          a = 1;//set a to 1
    }
    if (num == 2) {//end if//if user enters 2
          a = 2;//set a to 2
        }//end if
    System.out.println ( a);
    System.out.println(num);

    System.out.println ("Please enter a phrase: ");//asks user to enter phrase
    String message = myInput.readLine();//reads user input and assigns it to string

    int x = 0; //declares int var x

    System.out.println ("Your Encrypted code is: ");//prints out scentence


    while (x < message.length())//while loop will run while x is less that the phrase length          
    {
      String text = message.toUpperCase();//converts user input to upper case
      String letter = Character.toString(text.charAt(x));//extracts character from string and assigns it to another string letter

      x++;//increments x by 1 each time

      for(int i=0; i<letterA.length; i++)//for loop declares int i = 0, will run while i is less than the the length of the array letterA, and i will increment by 1 each time
      {
        if(letter.equals(letterA[a][i]))//if the letter is equal to letterA[i]
        {
          System.out.print (letterA[a][i]);//print out the corresponding letter

          break;//breaks from loop

        }//end if

        else if (letter.equals(" "))//else id the letter is equal to a space
        {
          System.out.print(" ");//prints out space
          break;//breaks from loop
        }//end else if        
      }//end for loop
    }//end while loop
  }//end main
}//end class
4

2 回答 2

2

这不起作用,因为letterA.length是 3,所以你的for循环只运行 3 次迭代,而不是 26 次。

于 2013-09-13T21:48:18.930 回答
0

我认为您应该将 for 循环更改为

    for (int i= 0; i < letterA[0].length ; i++ ) {
           if (letter.equals(letterA[0][i]) {
               System.out.print(letterA[a][i]);
               break;              
           }
           else {
                // .........,.......
           }
    }

首先使用第一个数组作为基础。比较字母,如果它们相等,则获取该字母的索引以用于加密

我现在正在使用该死的电话 ryt 所以我并没有真正编译你的代码

于 2013-09-13T22:43:24.873 回答