在這個學校的小項目中,我正在做一個凱撒密碼。將要做的是,用戶將打出一個單詞,它將被轉換爲一個字符數組,然後轉換爲相應的ASCII數字。然後這個方程將在每個號碼來執行:將ascii數字數組轉換爲它們各自的字符
new_code =(Ascii_Code +移用戶挑選出一個數])%26
到目前爲止,這裏是我已經寫出的代碼:
import javax.swing.*;
import java.text.*;
import java.util.*;
import java.lang.*;
public class Encrypt {
public static void main(String[] args) {
String phrase = JOptionPane.showInputDialog(null, "Enter phrase to be messed with ");
String shift = JOptionPane.showInputDialog(null, "How many spots should the characters be shifted by?");
int shiftNum = Integer.parseInt(shift); //converts the shift string into an integer
char[] charArray = phrase.toCharArray(); // array to store the characters from the string
int[] asciiArray = new int[charArray.length]; //array to store the ascii codes
//for loop that converts the charArray into an integer array
for (int count = 0; count < charArray.length; count++) {
asciiArray[count] = charArray[count];
System.out.println(asciiArray[count]);
} //end of For Loop
//loop that performs the encryption
for (int count = 0; count < asciiArray.length; count++) {
asciiArray[count] = (asciiArray[count]+ shiftNum) % 26;
} // end of for loop
//loop that converts the int array back into a character array
for (int count = 0; count < asciiArray.length; count++) {
charArray[count] = asciiArray[count]; //error is right here =(
}
}//end of main function
}// end of Encrypt class
它提到了最後一個for循環中的「可能的精度損失」。還有什麼我應該做的嗎?謝謝!
因此,就像翻譯多次從英語翻譯成日語一樣,將原來的短語完全改變爲其他語言,因爲它們是兩種不同的語言? – user1768884
@ user1768884,是的。如果你循環翻譯「我吃**蘋果」。在英語和沒有確定(「the」)和不確定(「a」)條款的語言之間,你可能會得到「我吃了**蘋果」。背部。這是精確度的損失。 –