第一次來這裏。我試圖編寫一個程序,它接受來自用戶的字符串輸入並使用replaceFirst方法對其進行編碼。除「`」(格雷夫重音)以外的所有字母和符號都可以正確編碼和解碼。replaceFirst字符 「`」
例如當我輸入
`12
我應該得到28AABB作爲我的加密,而是,它給了我BB8AA2
public class CryptoString {
public static void main(String[] args) throws IOException, ArrayIndexOutOfBoundsException {
String input = "";
input = JOptionPane.showInputDialog(null, "Enter the string to be encrypted");
JOptionPane.showMessageDialog(null, "The message " + input + " was encrypted to be "+ encrypt(input));
public static String encrypt (String s){
String encryptThis = s.toLowerCase();
String encryptThistemp = encryptThis;
int encryptThislength = encryptThis.length();
for (int i = 0; i < encryptThislength ; ++i){
String test = encryptThistemp.substring(i, i + 1);
//Took out all code with regard to all cases OTHER than "`" "1" and "2"
//All other cases would have followed the same format, except with a different string replacement argument.
if (test.equals("`")){
encryptThis = encryptThis.replaceFirst("`" , "28");
}
else if (test.equals("1")){
encryptThis = encryptThis.replaceFirst("1" , "AA");
}
else if (test.equals("2")){
encryptThis = encryptThis.replaceFirst("2" , "BB");
}
}
}
我試圖把轉義字符的重音的前面,但是,它仍然沒有正確編碼。
你真的有一個'else'沒有' if'? –
你的代碼不能編譯。發佈您正在使用的真實代碼會更有幫助。 – Pshemo
我刪除了所有其他情況,只顯示這個特定的情況。 – user2635709