简体   繁体   English

使用 switch 语句时如何将用户返回到主菜单? Java

[英]How to return user to main menu when using switch statement? Java

I am creating a program that will allow the user to enter a UK airport and an international airport to assess the feasibility of running a flight between them.我正在创建一个程序,允许用户进入英国机场和国际机场,以评估在它们之间运行航班的可行性。

I have created a text file that contains the international airport info and read this into an ArrayList for flexibility.我创建了一个包含国际机场信息的文本文件,并将其读入ArrayList以获得灵活性。

I want to return the user to the main menu if the user does not enter a valid international airport.如果用户没有输入有效的国际机场,我想将用户返回到主菜单。 I have used switch statement for the menu choices.我已将switch语句用于菜单选择。

File contents/Code/input/output are as shown below.文件内容/代码/输入/输出如下图所示。

File Contents:文件内容:

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

Code:代码:

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
        /*File file = new File("Airports.txt");
        Scanner fileScan = new Scanner(file);
        System.out.println(fileScan.nextLine());
        //getting all lines from text file
        while(fileScan.hasNextLine()) {
            System.out.println(fileScan.nextLine());
        } */
        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; // *** where I want to return user to main menu**
            } while (ukCode.equals("LPL") || ukCode.equals("BOH"));
        }
    }
}

Input:输入:

------------------------------------------------------
| Enter 1 to input airport details                   |
| Enter 2 to input flight details                    |
| Enter 3 to enter price plan and calculate profit   |
| Enter 4 to clear data                              |
| Enter 5 to quit                                    |
------------------------------------------------------

[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]

Enter menu choice: 
1
You selected >Enter airport details<

Enter code for UK airport

LPL

Enter valid code for overseas airport

GHGH **(not valid - doesnt exist in text file)**

Not valid input

Enter valid code for overseas airport //**instead of this i want user to return to main menu**

try it this way:试试这种方式:

    ...
    ...
    break; // where I want to return user to main menu
  } while (ukCode.equals("LPL") || ukCode.equals("BOH"));
   menu();

This would make your do-while loop absolutely futile, but you would get what you want.这将使您的 do-while 循环绝对徒劳,但您会得到您想要的。 Look into the documentation about loops to enhance your code... https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html查看有关循环的文档以增强您的代码... https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.ZFC35FDC70D5FC69D269883A822C7A53

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM