簡體   English   中英

Java程序中的2D數組

[英]2D Arrays in Java Program

我的程序應該從用戶那里獲得一個短語,然后向用戶返回他們輸入的短語的加密代碼(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

這不起作用,因為letterA.length是3,所以你的for循環只運行3次迭代,而不是26次。

我認為你應該改變你的for循環

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

首先使用第一個數組作為基礎。 比較字母然后如果它們相等則獲得該字母的索引以用於加密

我現在正在使用血腥手機,所以我沒有真正編譯你的代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM