题解 | #简单密码#
简单密码
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 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String input = in.nextLine();
System.out.println(getKey(input));
}
}
public static String getKey(String input) {
String result = "";
for (int i = 0; i < input.length(); i++) {
switch (input.charAt(i)) {
case 'a':
case 'b':
case 'c':
result += '2';
break;
case 'd':
case 'e':
case 'f':
result += '3';
break;
case 'g':
case 'h':
case 'i':
result += '4';
break;
case 'j':
case 'k':
case 'l':
result += '5';
break;
case 'm':
case 'n':
case 'o':
result += '6';
break;
case 'p':
case 'q':
case 'r':
case 's':
result += '7';
break;
case 't':
case 'u':
case 'v':
result += '8';
break;
case 'w':
case 'x':
case 'y':
case 'z':
result += '9';
break;
default:
break;
}
if (input.charAt(i) >= 'A' && input.charAt(i) <= 'Y') {
result += (char)(33 + input.charAt(i));
continue;
}
if (input.charAt(i) >= '0' && input.charAt(i) <= '9') {
result += input.charAt(i);
continue;
}
if (input.charAt(i) == 'Z') {
result += 'a';
}
}
return result;
}
}

查看12道真题和解析