我正在處理一個簡單的密碼,將字母表中的每個字母切換到其對應的三個空格處。我首先通過使用一種方法來加密密碼,現在正在解密密碼。不幸的是,除了abc
之外,每封信都有效,它會返回一個撇號,一個連字符和一個空格。如何讓循環跳過ASCII中的符號並設法讓它從A循環回Z?如何修復我的方法,使其僅返回字母而不是符號?
public static String caesarDecrypt(String encoded, int shift){
String decrypted = "";
for (int i = 0; i < encoded.length(); i++) {
char t = encoded.charAt(i);
if ((t >= 'a') && (t <= 'z')) {
t -= shift;
}
if (t > 'z') {
t += 26;
} else if ((t >= 'A') && (t <= 'Z')) {
{
t -= shift;
}
if (t > 'Z')
t += 26;
} else {
}
decrypted = decrypted + t;
}
return decrypted;
}
請說明您確保如何確保結果僅由字母組成。 –
'「爲什麼我的方法在將它限制爲字母時會返回符號?」 - 受限制** **字母是什麼? –
@ PM77-1我假設我的if語句確保我的回報只會是字母。當我使用t + = shift和t - = 26時,該方法只返回字符 –