简体   繁体   中英

Need help creating a loop that allows the user to interact with the menu until they signal they want to exit the system. (Java)

For the life of me I cannot figure out how to start creating this while loop to do what they are asking. It tells me to "add loop and code here that accepts and validates user input and takes the appropriate action. include appropriate user feedback and re-display the menu as needed." The menu that the user will choose from is as follows:

[1] Add Ship          [A] Print Ship Name
[2] Edit Ship         [B] Print Ship In Service List
[3] Add Cruise        [C] Print Ship Full List
[4] Edit Cruise       [D] Print Cruise List
[5] Add Passenger     [E] Print Cruise Details
[6] Edit Passenger    [F] Print Passenger List
[x] Exit System

The code goes within the main method shown below:

public static void main(String[] args) {

    initializeShipList();       // initial ships
    initializeCruiseList();     // initial cruises
    initializePassengerList();  // initial passengers

    // add loop and code here that accepts and validates user input
    // and takes the appropriate action. include appropriate
    // user feedback and redisplay the menu as needed


}

I hope you need something like this, here you will perform a specific task based on input provided and once current task done, ask user to do something more till your program will not receive signal("x") to exit.

public static void main(String args[]){

        System.out.print("[1] Add Ship          [A] Print Ship Name\r\n" + 
            "[2] Edit Ship         [B] Print Ship In Service List\r\n" + 
            "[3] Add Cruise        [C] Print Ship Full List\r\n" + 
            "[4] Edit Cruise       [D] Print Cruise List\r\n" + 
            "[5] Add Passenger     [E] Print Cruise Details\r\n" + 
            "[6] Edit Passenger    [F] Print Passenger List\r\n" + 
            "[x] Exit System" + "\r\nEnter your choice from the above menu:" 
            );
        Scanner sc = new Scanner(System.in);
        String choice = sc.nextLine();

        while(!choice.equals('x')){
            switch(choice){
                case "1":{
                   initializeShipList(); //Code for adding the Ship
                   System.out.println("test");  
                   break; 
                }
                case "2":{
                   editShipList();//Code for editing the Records
                   break;
                }
                case "3":{
                   initializeCruiseList//Code for editing the Records
                   break;
                }
                //here other option and so on..

                case "A":{
                   //Code for Print ship name 
                   break;
                }
                //here other option and so on..

                case "x":{
                   System.exit(0); 
                }
            }

            System.out.print("Do something more? Please select option: ");
            choice = sc.nextLine();
        }
}

Use this:

Scanner sc=new Scanner(System.in);
String input=sc.nextLine();
while(!input.equals('x')){
        // do your stuff
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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