2016-12-04 28 views
-2

所以,我想要做的是註冊,所以步驟是:Java中的字符陣列

用4個字母的單詞閱讀。

將字符串解析爲字符並將字符轉換爲整數。將這些十進制值存儲在數組中。

乘以您的加密矩陣

打印編碼的字。

乘以加密矩陣的逆矩陣,並打印出出來

但是,我有上打印所述編碼字的麻煩的步驟。在我印刷的最底部,這個詞沒有出現。有什麼我做錯了嗎?

public static void main(String [] args){ 

Scanner input = new Scanner(System.in); 
String word = "give"; { 

    while(word.length() == 4){ 
     word=word;} 

    while(word.length() != 4){ 
     word=input.next(); 
     } 


    int[][] wordArray = new int[2][2]; 
    wordArray[0][0] = (int)word.charAt(1); 
    wordArray[0][1] = (int)word.charAt(2); 
    wordArray[1][0] = (int)word.charAt(3); 
    wordArray[1][1] = (int)word.charAt(4); 

    int[][] encriptionArray = new int [2][2]; 
    encriptionArray[0][0] = 1; 
    encriptionArray[0][1] = 2; 
    encriptionArray[1][0] = 3; 
    encriptionArray[1][1] = (4); 


    int[][] printArray = new int [2][2]; 
    printArray[0][0]= wordArray[0][0]*encriptionArray[0][0]+ wordArray[0][1]*encriptionArray[0][1]; 
    printArray[0][1]= wordArray[0][1]*encriptionArray[0][1]+ wordArray[0][1]*encriptionArray[1][1]; 
    printArray[1][0]= wordArray[1][0]*encriptionArray[0][0]+ wordArray[1][1]*encriptionArray[1][0]; 
    printArray[1][1]= wordArray[0][1]*encriptionArray[1][0]+ wordArray[1][1]*encriptionArray[0][1]; 

    System.out.print(printArray[0][0]); 
    System.out.print(printArray[0][1]); 
    System.out.print(printArray[1][0]); 
    System.out.print(printArray[1][1]); 

} 


} 

}

+0

是有什麼錯我的問題? – user7214716

回答

0
String word = "give"; 

while(word.length() == 4){ 
    word=word; 
} 

即是一個無限循環。 word = word;什麼都不做。由於單詞長度爲4,while循環連續執行。學習使用你的調試器,並逐行執行你的代碼,看看它在做什麼。

0

while循環更改爲這樣的事情:

while (true) { 
    System.out.print("Enter 4-letter word:"); 
    word = input.nextLine(); 
    if(word.length()==4){ 
     break; 
    } 
}