题解 | #简单密码#
简单密码
https://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac
import java.lang.String; import java.util.HashMap; import java.util.Scanner; import java.util.Set; import java.util.Map; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String from = scanner.nextLine(); String s = "abcdefghijklmnopqrstuvwxyz"; HashMap<String, String> hashMap = new HashMap<>(); hashMap.put("abc", "2"); hashMap.put("def", "3"); hashMap.put("ghi", "4"); hashMap.put("jkl", "5"); hashMap.put("mno", "6"); hashMap.put("pqrs", "7"); hashMap.put("tuv", "8"); hashMap.put("wxyz", "9"); for (int i = 0; i < from.length(); i++) { char c = from.charAt(i); if (Character.isUpperCase(c)) { //大写字母 char newC = Character.toLowerCase(c); System.out.print(s.charAt((s.indexOf(newC) + 1) % s.length())); } else if (Character.isLowerCase(c)) { //小写字母 Set<Map.Entry<String, String>> entries = hashMap.entrySet(); for (Map.Entry<String, String> entry : entries) { if (entry.getKey().indexOf(c) > -1) { System.out.print(entry.getValue()); break; } } } else { System.out.print(c); } } } }