how to make a java main menu loop after using a case

Solutions on MaxInterview for how to make a java main menu loop after using a case by the best coders in the world

showing results for - "how to make a java main menu loop after using a case"
Minerva
04 Sep 2020
1do{
2  //Menu options
3  System.out.print("6.) Exit\n");
4  System.out.print("\nEnter Your Menu Choice: ");
5
6  choice = input.nextInt();
7
8  switch(choice){
9        //Your cases from 1 to 6.
10        default:
11            System.out.println("Invalid menu choice; try again.");
12            break;
13   }
14}while(choice != 6);
15
Annette
17 May 2019
1int choiceentry;
2
3do {
4    System.out.println("Enter \"1\", \"2\" or \"3\"");
5    choiceentry = scanchoice.nextInt();
6
7    switch (choiceentry)
8    {
9        case 1:
10            // do something
11            break;
12        case 2: 
13            // ..something else
14            break;
15        case 3: 
16            // .. exit program
17            break;
18        default:
19            System.out.println("Choice must be a value between 1 and 3.");
20    }   
21} while (choiceentry != 3);
22
Yohann
22 Oct 2016
1import java.util.Scanner;
2public class basicCalc {
3//How to make a Java Main Menu Loop after using a case
4public static void main(String[] args) {
5    // TODO Auto-generated method stub
6
7    Scanner input = new Scanner(System.in);
8    boolean mainLoop = true;
9
10    int choice;
11    while(true){
12        System.out.println("Calculator Main Menu\n");
13        System.out.print("1.) Addition \n");
14        System.out.print("2.) Subtraction.\n");
15        System.out.print("3.) Multiplication.\n");
16        System.out.print("4.) Division.\n");
17        System.out.print("5.) Generate Random Number.\n");
18        System.out.print("6.) Exit\n");
19        System.out.print("\nEnter Your Menu Choice: ");
20
21        choice = input.nextInt();
22
23
24    switch(choice){
25
26    case 1:
27        //Definitions
28        int adNumf, adNuml, sum;
29        System.out.print("Please Enter The First Number: ");
30        adNumf = input.nextInt();
31        System.out.print("\nPlease Enter The Second Number: ");
32        adNuml = input.nextInt();
33        sum = adNumf + adNuml;
34        System.out.print("The Sum Of Those Numbers is: " +sum);
35        break;
36
37    case 2: 
38        int subNum1, subNum2, sum2;
39        System.out.println("\nPlease Enter The First Number: ");
40        subNum1 = input.nextInt();
41        System.out.println("Please Enter The Second Number: ");
42        subNum2 = input.nextInt();
43        sum2 = subNum1 - subNum2;
44        System.out.println("The Subtraction Leaves The Number: " +sum2);
45        break;
46
47    case 3:
48        int multNum1, multNum2, multTotal;
49
50        // Gather Input
51        System.out.println("Please Enter The First Number To Multiply: ");
52        multNum1 = input.nextInt();
53        System.out.println("Please Enter The Second Number To Multiply: ");
54        multNum2 = input.nextInt();
55
56        // This will Multiply the Numbers
57        multTotal = multNum1 * multNum2;
58
59        //Display Final
60        System.out.println("The Multiplied Numbers Are: " +multTotal);
61        break;
62
63    case 4: 
64        //Definitions
65        double divNum1, divNum2, divTotal;
66        System.out.println("Enter Your Numerator ");
67        divNum1 = input.nextInt();
68        System.out.println("Enter Your Denominator ");
69        divNum2 = input.nextInt();
70        if(divNum2 == 0){
71            System.out.println("Zero is Not divisable, please select a new denominator: ");
72            divNum2 = input.nextInt();
73        }
74        divTotal = divNum1 / divNum2;
75        System.out.println("Your divisor is: " +divTotal);
76        break;
77
78    case 5:
79        double limL, limH, rand;
80        System.out.println("Enter Your Low Limit: ");
81        limL = input.nextInt();
82        System.out.println("Enter Your High Limit ");
83        limH = input.nextInt();
84
85        //Equation to keep numbers within bounds
86        rand = limL + (Math.random() * ((limH - limL) + 1));
87        System.out.println("Given Your Limits, the Random Number will be: " +rand);
88        break;
89
90    case 6: 
91        System.out.println("Exiting Program...");
92        System.exit(0);
93         break;
94    default :
95             System.out.println("This is not a valid Menu Option! Please Select Another");
96             break;
97
98    }
99
100    }
101
102    }
103
104   }
105
Carl
04 Apr 2020
1import java.util.Scanner;
2public class basicCalc {
3
4    public static void main(String[] args) {
5        // TODO Auto-generated method stub
6
7        Scanner input = new Scanner(System.in);
8        boolean mainLoop = true;
9
10        int choice;
11        do{
12            System.out.println("Calculator Main Menu\n");
13            System.out.print("1.) Addition \n");
14            System.out.print("2.) Subtraction.\n");
15            System.out.print("3.) Multiplication.\n");
16            System.out.print("4.) Division.\n");
17            System.out.print("5.) Generate Random Number.\n");
18            System.out.print("6.) Exit\n");
19            System.out.print("\nEnter Your Menu Choice: ");
20            choice = input.nextInt();
21
22
23
24
25        switch(choice){
26
27        case 1:
28            //do something
29            break;
30
31        case 2: 
32            //do something
33            break;
34
35        case 3:
36            //do something
37            break;
38
39        case 4: 
40            //do something
41            break;
42
43        case 5:
44            //do something
45            break;
46
47        case 6: 
48            System.out.println("Exiting Program...");
49            System.exit(0);
50             break;
51        default:
52        System.out.println(choise + " is not a valid Menu Option! Please Select Another.");
53
54    }while(choice != 6 /*Exit loop when choice is 6*/);
55
56
57
58    }
59
60}
61
Erika
06 May 2016
1    //set choiceentry to -1, this will make it to enter while loop
2     int choiceentry = -1
3
4    while(choiceentry < 1 || choiceentry > 3){
5
6            System.out.println("Enter \"1\", \"2\", \"3\" or \"4\"");
7            if(scanchoice.hasNextInt())
8            choiceentry = scanchoice.nextInt();
9
10    }
11
12     switch(choiceentry){
13        case 1:
14           //do logic
15           break;
16        case 2:
17           //do logic
18           break;
19        case 3:
20           //do logic
21           break;
22   }
23