如果发生错误选择,如何循环菜单

How to loop a menu if wrong selection occurred

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

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 循环, 你能解释一下吗?

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

   }

} }

最好的方法是与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);
}
}

在您的程序中仅使用 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");
            }

        }

    }
}