简体   繁体   English

需要帮助创建一个循环,允许用户与菜单交互,直到他们发出想要退出系统的信号。 (爪哇)

[英]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.对于我的生活,我无法弄清楚如何开始创建这个 while 循环来完成他们的要求。 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.我希望你需要这样的东西,在这里你将根据提供的输入执行一个特定的任务,一旦当前任务完成,要求用户做更多的事情,直到你的程序不会收到信号(“x”)退出。

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
}

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

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