Search code examples
javaloopswhile-loopmenuprogram-entry-point

How to loop a menu if wrong selection occurred


I am trying to loop a menu when the user insert wrong choice:

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

    }

What I want is, if a user inserted wrong number, then the menu should start again, until the user enters a correct value (0 to 3). I am not sure where and how to put the while loop, Could you please clarify it?


Solution

  • Use only while and switch-case in your program.

    You can try the below code. It runs successfully.

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