第一次来这里。我试图编写一个程序,它接受来自用户的字符串输入并使用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