我是没看出来,这个明文和密文之间的关系,所以采用了硬编码的形式
public static void main(String[] args) {
Map<String, String> encryption = new HashMap<>(26);
// 明文 - 密文
encryption.put("A", "D");
encryption.put("B", "E");
encryption.put("C", "S");
encryption.put("D", "T");
encryption.put("E", "I");
encryption.put("F", "N");
// 一直写到 Z
Scanner scanner = new Scanner(System.in);
String next = scanner.nextLine();
String res = "";
for (int i = 0; i < next.length(); i++) {
if (encryption.get(next.charAt(i) + "") != null){
res += encryption.get(next.charAt(i) + "");
} else {
res += next.charAt(i);
}
}
System.out.println(res);
}
将字符串转成字符数组,对每个字符进行转换就行了