how to re desplay menu in java code example

Example 1: How to make a Java Main Menu Loop after using a case

int choiceentry;

do {
    System.out.println("Enter \"1\", \"2\" or \"3\"");
    choiceentry = scanchoice.nextInt();

    switch (choiceentry)
    {
        case 1:
            // do something
            break;
        case 2: 
            // ..something else
            break;
        case 3: 
            // .. exit program
            break;
        default:
            System.out.println("Choice must be a value between 1 and 3.");
    }   
} while (choiceentry != 3);

Example 2: how to make a java main menu loop after using a case

import java.util.Scanner;
public class basicCalc {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner input = new Scanner(System.in);
        boolean mainLoop = true;

        int choice;
        do{
            System.out.println("Calculator Main Menu\n");
            System.out.print("1.) Addition \n");
            System.out.print("2.) Subtraction.\n");
            System.out.print("3.) Multiplication.\n");
            System.out.print("4.) Division.\n");
            System.out.print("5.) Generate Random Number.\n");
            System.out.print("6.) Exit\n");
            System.out.print("\nEnter Your Menu Choice: ");
            choice = input.nextInt();




        switch(choice){

        case 1:
            //do something
            break;

        case 2: 
            //do something
            break;

        case 3:
            //do something
            break;

        case 4: 
            //do something
            break;

        case 5:
            //do something
            break;

        case 6: 
            System.out.println("Exiting Program...");
            System.exit(0);
             break;
        default:
        System.out.println(choise + " is not a valid Menu Option! Please Select Another.");

    }while(choice != 6 /*Exit loop when choice is 6*/);



    }

}

Tags:

Java Example