题解 | #配置文件恢复#

配置文件恢复

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";
    }

}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务