题解 | #配置文件恢复#
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sn = new Scanner(System.in); Map<String, String> map = new HashMap<>(); map.put("reset", "reset what"); map.put("reset board", "board fault"); map.put("board add", "where to add"); map.put("board delete", "no board at all"); map.put("reboot backplane", "impossible"); map.put("backplane abort", "install first"); while (sn.hasNext()) { String s = sn.nextLine(); String s1 = finCom(s, map); System.out.println(s1); } } private static String finCom(String s, Map<String, String> map) { String[] s1 = s.split(" "); if (s1.length == 1) { if ("reset".startsWith(s1[0])) { return map.get("reset"); } } int count = 0; String rekey = null; if (s1.length == 2) { for (int i = 0; i < s1.length; i++) { Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, String> next = iterator.next(); String key = next.getKey(); String [] keyArray = key.split(" "); if (keyArray.length == 2 && keyArray[0].startsWith(s1[0]) && keyArray[1].startsWith(s1[1])) { count++; rekey = key; } } if (count == 1) { return map.get(rekey); } else { return "unknown command"; } } } else { return "unknown command"; } return "unknown command"; } }