How to make a Java Main Menu Loop after using a case

You can give whole code(from displaying menu) inside a while loop and give condition as true so that after using a case it will automatically repeat(as you are using 6 to EXIT). And if any invalid input is given for eg:10 the case will go to default section and will execute the code there

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;
    while(true){
        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:
        //Definitions
        int adNumf, adNuml, sum;
        System.out.print("Please Enter The First Number: ");
        adNumf = input.nextInt();
        System.out.print("\nPlease Enter The Second Number: ");
        adNuml = input.nextInt();

Leave a Comment