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

问题描述 投票:0回答:1

我想在用户输入错误的选项时,循环一个菜单,我想的是,如果用户输入了错误的数字,那么菜单应该重新开始,直到用户输入一个正确的值(0到3)。

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个回答
0
投票

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

        }

    }
}

0
投票

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

-1
投票

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

   }

} }

© www.soinside.com 2019 - 2024. All rights reserved.