题解 | #配置文件恢复#

配置文件恢复

https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        HashMap<String, String> hm = new HashMap<>();
        hm.put("reset", "reset what");
        hm.put("reset board", "board fault");
        hm.put("board add", "where to add");
        hm.put("board delete", "no board at all");
        hm.put("reboot backplane", "impossible");
        hm.put("backplane abort", "install first");

        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String ins = in.nextLine();
            String a[] = ins.split(" ");
            if (a.length == 1) {
                if (hm.get("reset").startsWith(a[0])) {
                    System.out.println(hm.get("reset"));
                } else {
                    System.out.println("unknown command");
                }
            } else if (a.length == 2) {
                int num = 0;
                ArrayList<String> out = new ArrayList<>();
                hm.keySet().stream().filter(s -> s.contains(" ")).forEach(s-> {
                    String[] ss = s.split(" ");
                    if (ss[0].startsWith(a[0]) && ss[1].startsWith(a[1])) {
                        out.add(hm.get(s));
                    }
                });
                if (out.size() == 1) {
                    System.out.println(out.get(0));
                } else {
                    System.out.println("unknown command");
                }
            }

        }
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务