Java中使用switch语句时,输入无效机场代码如何返回主菜单?
问题:输入无效海外机场代码时返回主菜单
我正在开发一个评估两地航班开通可行性的程序,允许用户输入英国机场和国际机场代码。目前已经把国际机场信息从文本文件读取到集合里,用switch处理菜单选择。现在的需求是:当用户输入无效的国际机场代码时,直接返回主菜单,而不是继续提示用户重新输入。
当前代码里,无效输入后会继续循环让用户输入,但我希望触发无效输入时直接回到主菜单。
现有文件与代码
Airports.txt 文件内容:
JFK, John F Kennedy International, 5326, 5486, ORY, Paris-Orly, 629, 379, MAD, Adolfo Suarez Madrid-Baranjas, 1428, 1151, AMS, Amsterdam Schipol, 526, 489, CAI, Cairo International, 3779, 3584
现有Java代码:
import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.ArrayList; import java.io.IOException; class Main { // menu method static void menu() { System.out.println("------------------------------------------------------"); System.out.println("| Enter 1 to input airport details |"); System.out.println("| Enter 2 to input flight details |"); System.out.println("| Enter 3 to enter price plan and calculate profit |"); System.out.println("| Enter 4 to clear data |"); System.out.println("| Enter 5 to quit |"); System.out.println("------------------------------------------------------"); } public static void main(String[] args) throws IOException { System.out.println("Welcome..."); System.out.println(); menu(); System.out.println(); // reading file ArrayList<String> airportList = new ArrayList<>(); try (Scanner scan = new Scanner(new FileReader("Airports.txt"))) { while (scan.hasNext()) { airportList.add(scan.nextLine().replace(",", "").trim()); } System.out.println(airportList); } Scanner scanner = new Scanner(System.in); System.out.println("\n" + "Enter menu choice: "); int menuChoice = scanner.nextInt(); switch (menuChoice) { case 5: System.out.println("You selected >Quit<. Program ending..."); break; case 1: System.out.println("You selected >Enter airport details<"); System.out.println("\n" + "Enter code for UK airport"); String ukCode = scanner.next(); do { System.out.println("Enter valid code for overseas airport"); String overseasCode = scanner.next(); if (airportList.contains(overseasCode) && overseasCode.equals("JFK")) { System.out.println("John F Kennedy International"); break; } else if (airportList.contains(overseasCode) && overseasCode.equals("ORY")) { System.out.println("Paris-Orly"); break; } else if (airportList.contains(overseasCode) && overseasCode.equals("MAD")) { System.out.println("Adolfo Suarez Madrid-Baranjas"); break; } else if (airportList.contains(overseasCode) && overseasCode.equals("AMS")) { System.out.println("Amsterdam Schipol"); break; } else if (airportList.contains(overseasCode) && overseasCode.equals("CAI")) { System.out.println("Cairo International"); break; } else { System.out.println("Not valid input"); // 这里希望直接返回主菜单,而不是继续循环 } continue; } while (ukCode.equals("LPL") || ukCode.equals("BOH")); } } }
当前输入输出问题:
当输入无效代码(比如
GHGH)时,程序会提示Not valid input然后再次要求输入海外机场代码,但我希望此时直接回到主菜单。
解决方案:
要实现这个需求,我们可以做两个关键修改:
- 重构机场数据存储:当前的
airportList把所有数据拆成了单个字符串,判断代码是否存在很麻烦。建议用HashMap<String, String>存储(键是机场代码,值是机场名称),查找更高效,代码也更简洁。 - 修改循环逻辑:添加主程序循环,让操作完成后自动回到主菜单;检测到无效输入时,直接跳出当前操作流程。
修改后的完整代码:
import java.util.Scanner; import java.io.FileReader; import java.util.HashMap; import java.io.IOException; class Main { // 菜单方法 static void menu() { System.out.println("------------------------------------------------------"); System.out.println("| Enter 1 to input airport details |"); System.out.println("| Enter 2 to input flight details |"); System.out.println("| Enter 3 to enter price plan and calculate profit |"); System.out.println("| Enter 4 to clear data |"); System.out.println("| Enter 5 to quit |"); System.out.println("------------------------------------------------------"); } public static void main(String[] args) throws IOException { // 用HashMap存储机场代码与名称的对应关系,查找更高效 HashMap<String, String> airportMap = new HashMap<>(); // 读取并解析机场文件 try (Scanner scan = new Scanner(new FileReader("Airports.txt"))) { String[] airportData = scan.nextLine().split(","); // 每4个元素为一组机场数据:代码、名称、两个数字 for (int i = 0; i < airportData.length; i += 4) { String code = airportData[i].trim(); String name = airportData[i+1].trim(); airportMap.put(code, name); } } Scanner scanner = new Scanner(System.in); boolean isRunning = true; // 主循环:保持程序运行直到用户选择退出 while (isRunning) { System.out.println("\nWelcome..."); menu(); System.out.println("\nEnter menu choice: "); // 处理非整数的无效菜单输入 while (!scanner.hasNextInt()) { System.out.println("请输入有效的数字选项!"); scanner.next(); // 清空无效输入 } int menuChoice = scanner.nextInt(); switch (menuChoice) { case 5: System.out.println("You selected >Quit<. Program ending..."); isRunning = false; break; case 1: System.out.println("You selected >Enter airport details<"); System.out.println("\nEnter code for UK airport"); String ukCode = scanner.next().trim(); // 验证英国机场代码(仅接受LPL和BOH) if (!ukCode.equals("LPL") && !ukCode.equals("BOH")) { System.out.println("无效的英国机场代码,返回主菜单"); break; } System.out.println("Enter valid code for overseas airport"); String overseasCode = scanner.next().trim(); // 检查海外机场代码有效性 if (airportMap.containsKey(overseasCode)) { System.out.println(airportMap.get(overseasCode)); } else { System.out.println("Not valid input - 返回主菜单"); } // 操作完成后自动回到主循环的菜单 break; case 2: System.out.println("You selected >Enter flight details<"); // 后续可添加航班信息输入逻辑 break; case 3: System.out.println("You selected >Enter price plan and calculate profit<"); // 后续可添加利润计算逻辑 break; case 4: System.out.println("You selected >Clear data<"); airportMap.clear(); System.out.println("数据已清空"); break; default: System.out.println("无效的菜单选项,请重新选择"); } } scanner.close(); } }
关键修改说明:
HashMap存储机场数据:替代原来的ArrayList,避免了冗长的if-else判断,查找效率更高,后续新增机场也更方便。- 主循环
while(isRunning):让程序可以持续运行,用户完成任意操作后都会自动回到主菜单,无需重启程序。 - 简化无效输入处理:当检测到无效的海外机场代码时,直接提示并跳出当前case,回到主循环重新显示菜单。
- 增加输入校验:处理用户输入非整数菜单选项的情况,避免程序崩溃。
这样修改后,当用户输入无效的海外机场代码时,程序会提示错误并直接回到主菜单,完全符合需求。
内容的提问来源于stack exchange,提问作者user177429479
相关产品推荐
相关产品推荐

