题解 | #简单密码#
简单密码
https://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
String code = in.nextLine();
char[] code1 = code.toCharArray();
StringBuffer str = new StringBuffer();
for (int i = 0; i < code.length(); i++) {
if (Character.isLetter(code1[i])) {
str.append(transferCharcter(code1[i]));
} else {
str.append(code1[i]);
}
}
System.out.println(str.toString());
}
//加密字母
public static char transferCharcter(char charc) {
char res = 'a';
if (charc < 'a') {
if (charc != 'Z') {
res = (char)(charc - 'A' + 'a' + 1);
} else {
res = 'a';
}
} else {
if (charc >= 'a' && charc <= 'c') {
res = '2';
} else if (charc >= 'd' && charc <= 'f') {
res = '3';
} else if (charc >= 'g' && charc <= 'i') {
res = '4';
} else if (charc >= 'j' && charc <= 'l') {
res = '5';
} else if (charc >= 'm' && charc <= 'o') {
res = '6';
} else if (charc >= 'p' && charc <= 's') {
res = '7';
} else if (charc >= 't' && charc <= 'v') {
res = '8';
} else {
res = '9';
}
}
return res;
}
}