簡體   English   中英

使用 switch 語句時如何將用戶返回到主菜單? Java

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

我正在創建一個程序,允許用戶進入英國機場和國際機場,以評估在它們之間運行航班的可行性。

我創建了一個包含國際機場信息的文本文件,並將其讀入ArrayList以獲得靈活性。

如果用戶沒有輸入有效的國際機場,我想將用戶返回到主菜單。 我已將switch語句用於菜單選擇。

文件內容/代碼/輸入/輸出如下圖所示。

文件內容:

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

代碼:

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"));
        }
    }
}

輸入:

------------------------------------------------------
| 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**

試試這種方式:

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

這將使您的 do-while 循環絕對徒勞,但您會得到您想要的。 查看有關循環的文檔以增強您的代碼... https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.ZFC35FDC70D5FC69D269883A822C7A53

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM