题解 | #配置文件恢复#
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] first = {"reset", "reset", "board", "board", "reboot", "backplane"}; String[] second = {"reset what", "board board fault", "add where to add", "delete no board at all", "backplane impossible", "abort install first"}; while (in.hasNext()) { String str = in.nextLine(); if (!str.contains(" ")) { //单字符命令就一个,这里取巧直接输出 if (first[0].startsWith(str)) { System.out.println(second[0]); } else { System.out.println("unknown command"); } } else { //输入两字串,遍历两数组,共用同一个索引 String[] opStr = str.split(" "); int count = 0; int index = 0; for (int i = 0; i < first.length; i++) { if (first[i].startsWith(opStr[0]) && second[i].startsWith(opStr[1])) { count++; index = i; //记录匹配的次数和索引 } } if (count == 1) { int spaceBegin = second[index].indexOf(" ", 0); //找到第一个空格的位置,输出之后的子串 System.out.println(second[index].substring(spaceBegin + 1)); } else { System.out.println("unknown command"); } } } } }