题解 | #简单密码#
简单密码
https://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac
import java.util.HashMap;
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 s=in.nextLine();
char[] ch=s.toCharArray();
HashMap<String,String> map=new HashMap<>();
map.put("a", "2");
map.put("b", "2");
map.put("c", "2");
map.put("d", "3");
map.put("e", "3");
map.put("f", "3");
map.put("g", "4");
map.put("h", "4");
map.put("i", "4");
map.put("j", "5");
map.put("k", "5");
map.put("l", "5");
map.put("m", "6");
map.put("n", "6");
map.put("o", "6");
map.put("p", "7");
map.put("q", "7");
map.put("r", "7");
map.put("s", "7");
map.put("t", "8");
map.put("u", "8");
map.put("v", "8");
map.put("w", "9");
map.put("x", "9");
map.put("y", "9");
map.put("z", "9");
map.put("0", "0");
//遍历字符串
for (char c : ch) {
if (c >= 'A' && c <= 'Y') {
char newChar = (char)(Character.toLowerCase(c) + 1);
System.out.print(newChar);
} else if (c >= 'a' && c <= 'z') {
String key = String.valueOf(c);
// if (map.containsKey(c)) {
System.out.print(map.get(key));
// }
} else if (c == 'Z') {
System.out.print('a');
} else if (c >= '0' && c <= '9') {
System.out.print(c);
}
}
}
}
腾讯公司福利 1159人发布