第一次来这里。我正在尝试编写一个程序,该程序从用户那里获取字符串输入并使用 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");
        }
    }
}
我尝试将转义字符放在重音前面,但是,它仍然没有正确编码。