.replace方法在java中不起作用
我想做一些加密,但当我输入a时,它不会变为I,它只会打印a。为什么会这样.replace方法在java中不起作用,java,java.util.scanner,Java,Java.util.scanner,我想做一些加密,但当我输入a时,它不会变为I,它只会打印a。为什么会这样 import java.util.Scanner; public class Encryption { public static void main(String[] args) { String normal = "", encoded, userResponse = ""; Scanner sc = new Scanner(System.in);
import java.util.Scanner;
public class Encryption
{
public static void main(String[] args)
{
String normal = "", encoded, userResponse = "";
Scanner sc = new Scanner(System.in);
System.out.println("Are you encoding or decoding? E or D");
userResponse = sc.next();
if (userResponse.equalsIgnoreCase("e"))
{
System.out.println("Enter the text that you want to encode:");
normal = sc.next();
encoded = normal.replace('a', 'i');
encoded = normal.replace('b', 's');
encoded = normal.replace('g', 'e');
encoded = normal.replace('k', 'f');
encoded = normal.replace('p', 'h');
encoded = normal.replace('c', 'z');
encoded = normal.replace('m', 'r');
encoded = normal.replace('n', 't');
encoded = normal.replace('o', 'd');
encoded = normal.replace('l', 'j');
System.out.println(encoded);
}
else if (userResponse.equalsIgnoreCase("d"))
{
}
}
}
由于在所有的
replace
调用中一直使用normal
,因此只有最后一个调用有效,因为它覆盖了所有其他更改
移除normal
变量,分配encoded=sc.next()
,并在每次调用replace
时使用encoded
,即
encoded = sc.next();
encoded = encoded.replace('a', 'i');
encoded = encoded.replace('b', 's');
encoded = encoded.replace('g', 'e');
... // And so on
encoded=normal.replace('l','j')代码>-此时,normal
包含什么?长“llllllllll…”的目的是什么?显然,这对大多数人来说是常识,因为我投了反对票,但感谢你的帮助!:)@布伦丹汉森:不客气!我不知道为什么人们对你的问题投了反对票,因为这并没有什么特别的问题:这是你通过犯这个特别的错误而获得的知识。