题解 | #简单密码# Java版本
简单密码
https://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String str = sc.nextLine(); StringBuilder sb = new StringBuilder(); for (char ch : str.toCharArray()) { sb.append(getRightChar(ch)); } System.out.println(sb.toString()); } } public static char getRightChar(char ch) { if (ch >= 'A' && ch <= 'Z') { if (ch == 'Z') { ch = 'a'; } else { ch += 33; // 大写A是65,小写a是97 } // 符合大写字母的 } else if (ch >= 'a' && ch <= 'z') { // 符合小写字母的 int num = ch - 'a'; ch = getNumChar(num); } return ch; } // 处理小写字母的变化 public static char getNumChar(int num) { char ch = '9'; if (num < 3) { ch = '2'; } else if (num < 6) { ch = '3'; } else if (num < 9) { ch = '4'; } else if (num < 12) { ch = '5'; } else if (num < 15) { ch = '6'; } else if (num < 19) { ch = '7'; } else if (num < 22) { ch = '8'; } return ch; } }