为什么不创建一个地图,其中区域是密钥,值是在该区域下的电台列表?
然后,您可以有一个处理地图的群体的方法...
private static Map<String, List<String>> createZoneMap() {
Map<String, List<String>> zoneMap = new HashMap<String, List<String>>();
// Probably want to populate this map from a file
return zoneMap;
}
然后你的主能看起来像......
public static void main(String args[]) {
Map<String, List<String>> zoneMap = createZoneMap();
Scanner scan = new Scanner(System.in);
String input;
while (true) {
input = scan.nextLine();
// Some code to exit the application...
if (input.equalsIgnoreCase("quit")) {
System.out.println("Exiting...");
System.exit(1);
}
String zone = findZone(zoneMap, input);
if (zone != null) {
System.out.println(input + " is in " + zone);
} else {
System.out.println("That is not a Station, please try again");
}
}
}
然后,当你在输入通过地图查找站名以查找该站所属的区域,如果它不存在则返回null或其他东西
private static String findZone(Map<String, List<String>> zoneMap, String station) {
// Maybe make this more versatile so that it does not care about case...
for (Map.Entry<String, List<String>> entry : zoneMap.entrySet()) {
if (entry.getValue().contains(station)) {
return entry.getKey();
}
}
return null;
}
希望这对你来说是一个很好的起点。你也可以考虑放弃在主要方法中执行所有的逻辑,而是在main中创建你的类的一个实例。
这与JavaScript无关。 – Griffith