【问题标题】:How to loop a menu if wrong selection occurred如果发生错误选择,如何循环菜单
【发布时间】:2020-09-15 19:26:24
【问题描述】:

当用户插入错误的选择时,我正在尝试循环菜单:

public static void main(String[] args) {

    Scanner YourChoice = new Scanner(System.in);

    System.out.println("+-------------------------+");
    System.out.println("|        Welcome          |");
    System.out.println("|    To this program      |");
    System.out.println("+-------------------------+");
    System.out.println();

    System.out.println("Please Choose your preference: ");
    System.out.println();
    System.out.println("Press 1 - Test1");
    System.out.println("Press 2 - Test2");
    System.out.println("Press 3 - Test3");
    System.out.println("Press 0 - Exit");


    System.out.println();
    System.out.print("Your Choice is: ");
    int CHOICE = YourChoice.nextInt();



    if (CHOICE == 1) {
        System.out.println("You want Test1"); 
    }
    else if (CHOICE == 2) {
        System.out.println("You want Test2"); 
    }
    else if (CHOICE == 3) {
        System.out.println("You want Test3"); 
    }
    else if (CHOICE == 0) {
        System.out.println("You want Exit"); 
    }
    else {
        System.out.println("Wrong Input, please try again");

    }

我想要的是,如果用户输入了错误的数字,那么菜单应该重新开始,直到用户输入正确的值(0 到 3)。 我不确定在哪里以及如何放置 while 循环, 你能澄清一下吗?

【问题讨论】:

    标签: java loops while-loop menu main


    【解决方案1】:

    在您的程序中仅使用 whileswitch-case

    你可以试试下面的代码。运行成功。

    import java.util.*;
    public class Main
    {
        public static void main(String[] args) {
    
            System.out.println("+-------------------------+");
            System.out.println("|        Welcome          |");
            System.out.println("|    To this program      |");
            System.out.println("+-------------------------+");
            System.out.println();
    
            Scanner yourChoice = new Scanner(System.in);
            int choice ; 
            boolean tryAgain = true;
    
            while (tryAgain){
    
                System.out.println("Please Choose your preference: ");
                System.out.println();
                System.out.println("Press 1 - Test1");
                System.out.println("Press 2 - Test2");
                System.out.println("Press 3 - Test3");
                System.out.println("Press 0 - Exit");
                System.out.println();
                choice = yourChoice.nextInt();
                System.out.print("\nYour Choice is : " +choice + "\n");
    
                switch(choice){
                    case 1 :
                        System.out.println("You want Test1");
                        tryAgain = false;
                        break;
                    case 2 :
                        System.out.println("You want Test2");
                        tryAgain = false;
                        break;
                    case 3 :
                        System.out.println("You want Test3");
                        tryAgain = false;
                        break;
                    case 0 :
                        System.out.println("You want exit");
                        tryAgain = false;
                        break;
                    default :
                        System.out.println("Wrong Input, please try again");
                }
    
            }
    
        }
    }
    

    【讨论】:

    • 和我的代码一样,为什么要发布另一个答案与相同的代码?
    • @Pluto 对不起。它与您的代码不同。有区别。请检查一次。另外,当我第一次发布答案时,我看不到您的答案..可能我们已经在类似的时间发布了。因为我先执行它然后发布答案。
    • @MohammedHashim:欢迎兄弟!如果你能接受我的回答,如果它对你有用。
    【解决方案2】:

    最好的方法是和switch case一起使用,它对代码效率有好处,和很多ifelse if一起使用不太好:

    Scanner YourChoice = new Scanner(System.in);
    
                System.out.println("+-------------------------+");
                System.out.println("|        Welcome          |");
                System.out.println("|    To this program      |");
                System.out.println("+-------------------------+");
                System.out.println();
    
                boolean flag = false;
    
                do {
                    System.out.println("Please Choose your preference: ");
                    System.out.println();
                    System.out.println("Press 1 - Test1");
                    System.out.println("Press 2 - Test2");
                    System.out.println("Press 3 - Test3");
                    System.out.println("Press 0 - Exit");
    
                    System.out.println();
                    System.out.print("Your Choice is: ");
                    int CHOICE = YourChoice.nextInt();
    
                    switch (CHOICE) {
                    case 0: {
                        System.out.println("You want Exit");
                        flag = true;
                        break;
                    }
                    case 1: {
                        System.out.println("You want Test1");
                        flag = true;
                        break;
                    }
                    case 2: {
                        System.out.println("You want Test2");
                        flag = true;
                        break;
                    }
                    case 3: {
                        System.out.println("You want Test3");
                        flag = true;
                        break;
                    }
                    default:{
                        System.out.println("Wrong Input, please try again");
                        break;
                        }
                    }
    
                } while (!flag);
    }
    }
    

    【讨论】:

    • case 语句中有很多不必要的大括号。这些不是必需的。
    【解决方案3】:

    public static void main(String[] args) {

    int CHOICE;
    Scanner YourChoice = new Scanner(System.in);
    
    System.out.println("+-------------------------+");
    System.out.println("|        Welcome          |");
    System.out.println("|    To this program      |");
    System.out.println("+-------------------------+");
    System.out.println();
    while(true)
    {
       System.out.println("Please Choose your preference: ");
       System.out.println();
       System.out.println("Press 1 - Test1");
       System.out.println("Press 2 - Test2");
       System.out.println("Press 3 - Test3");
       System.out.println("Press 0 - Exit");
    
    
       System.out.println();
       System.out.print("Your Choice is: ");
       CHOICE = YourChoice.nextInt();
    
       if (CHOICE == 1)
       {
          System.out.println("You want Test1"); 
       }
       else if (CHOICE == 2) 
       {
          System.out.println("You want Test2"); 
       }
       else if (CHOICE == 3) 
       {
          System.out.println("You want Test3"); 
       }
       else if (CHOICE == 0) 
       {
          System.out.println("You want Exit"); 
          System.exit(0);
       }
       else
       {
           System.out.println("Wrong Input, please try again");
    
       }
    

    } }

    【讨论】:

    • 谢谢您,您的解决方案有效,只是您忘记输入 break;在 1 到 3 之间,否则即使您键入 1、2 或 3,菜单也会重复
    • 不不,这就是我不使用 (break) 的原因,因为我必须重复菜单,因为如果用户想做另一件事,那么重复循环而不是一次又一次地开始很有用. (有时重复循环非常有用,当你制作另一个这种类型的程序时你会明白)。
    猜你喜欢
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 2016-08-24
    • 2022-06-18
    • 2015-01-29
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多