题解 | #配置文件恢复#不如先把命令拆开
配置文件恢复
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"); } } }