题解 | #配置文件恢复#不如先把命令拆开

配置文件恢复

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

import java.util.Objects;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        String[][] command = {
                {"reset", "", "reset what"},
                {"reset", "board", "board fault"},
                {"board", "add", "where to add"},
                {"board", "delete", "no board at all"},
                {"reboot", "backplane", "impossible"},
                {"backplane", "abort", "install first"},
                {"he", "he", "unknown command"}
        };
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String[] input = in.nextLine().split(" ");
            String output = "unknown command";
            int matchCount = 0;
            for (String[] strings : command) {
                if (input.length == 1) {
                    if (Objects.equals(strings[1], "")) {
                        if (strings[0].startsWith(input[0])) {
                            output = strings[2];
                            matchCount++;
                        }
                    }
                } else {
                    if (strings[0].startsWith(input[0]) && strings[1].startsWith(input[1])) {
                        output = strings[2];
                        matchCount++;
                    }
                }
            }
            System.out.println(matchCount == 1 ? output : "unknown command");
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
09-29 17:44
已编辑
蔚来_测(准入职员工)
//鲨鱼辣椒:见不了了我实习了四个月上周再投筛选了一天就给我挂了
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务