题解 | #配置文件恢复#
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
//分情况匹配即可
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
Map<String, String> hashmap = new HashMap<>();
hashmap.put("reset board", "board fault");
hashmap.put("board add", "where to add");
hashmap.put("board delete", "no board at all");
hashmap.put("reboot backplane", "impossible");
hashmap.put("backplane abort", "install first");
while (in.hasNextLine()) {
String s = in.nextLine();
String[] ss = s.split(" ");
if (ss.length > 2) {
System.out.println("unknown command");
}
if (ss.length == 1) {
if (s.charAt(0) == 'r') {
if ("reset".contains(ss[0])) {
System.out.println("reset what");
}
else{
System.out.println("unknown command");
}
} else {
System.out.println("unknown command");
}
}
if (ss.length == 2) {
int count = 0;
String ans = "";
for (String key : hashmap.keySet()) {
String[] keys = key.split(" ");
if (keys[0].charAt(0) == ss[0].charAt(0) &&
keys[1].charAt(0) == ss[1].charAt(0)) {
if (keys[0].contains(ss[0]) && keys[1].contains(ss[1])) {
++count;
if (count == 1) {
ans = hashmap.get(key);
}
if (count == 2) {
System.out.println("unknown command");
break;
}
}
}
}
if (count == 1) {
System.out.println(ans);
} else if (count == 0) {
System.out.println("unknown command");;
}
}
}
}
}