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

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 に答える