题解 | #配置文件恢复#

配置文件恢复

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static void main(String[] args) {

        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");
        Set<String> keys = map.keySet();
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String str = scanner.nextLine();
            String[] receive = str.split(" ");
            List<String> list = new ArrayList<>();
            List<String> errList = new ArrayList<>();
            for (String value : keys) {
                String[] key = value.split(" ");
                if(receive.length==2){
                    if(key.length==2){
                      if(key[0].startsWith(receive[0])&&key[1].startsWith(receive[1])){
                          list.add(value);
                          continue;
                      }
                    }else{
                        continue;
                    }
                }else{
                    if(key.length==1){
                        if(key[0].startsWith(receive[0])){
                            list.add(value);
                            continue;
                        }

                    }else if(key[0].startsWith(receive[0])){
                        errList.add(value);
                        continue;
                    }
                }
            }

            if(list.size()!=0){
                if(list.size()>1){
                    System.out.println("unknown command");
                }else{
                    System.out.println(map.get(list.get(0)));
                }

            }else if(errList.size()>0){
                System.out.println("unknown command");
            }else{
                System.out.println("unknown command");
            }
        }
    }
}
全部评论

相关推荐

今天刚通知oc
跑不快的yyyf:接好运
点赞 评论 收藏
分享
09-06 12:49
门头沟学院 Java
offeroffer...:我也是,前两面还挺紧张认真的,全程大脑飞速运转后面就越来越不想面了,不想说话不想思考
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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