Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

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
");
            System.out.print("1.) Addition 
");
            System.out.print("2.) Subtraction.
");
            System.out.print("3.) Multiplication.
");
            System.out.print("4.) Division.
");
            System.out.print("5.) Generate Random Number.
");
            System.out.print("6.) Exit
");
            System.out.print("
Enter 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*/);



    }

}
Comment

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

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

import java.util.Scanner;
public class basicCalc {
//How to make a Java Main Menu Loop after using a case
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
");
        System.out.print("1.) Addition 
");
        System.out.print("2.) Subtraction.
");
        System.out.print("3.) Multiplication.
");
        System.out.print("4.) Division.
");
        System.out.print("5.) Generate Random Number.
");
        System.out.print("6.) Exit
");
        System.out.print("
Enter 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("
Please Enter The Second Number: ");
        adNuml = input.nextInt();
        sum = adNumf + adNuml;
        System.out.print("The Sum Of Those Numbers is: " +sum);
        break;

    case 2: 
        int subNum1, subNum2, sum2;
        System.out.println("
Please Enter The First Number: ");
        subNum1 = input.nextInt();
        System.out.println("Please Enter The Second Number: ");
        subNum2 = input.nextInt();
        sum2 = subNum1 - subNum2;
        System.out.println("The Subtraction Leaves The Number: " +sum2);
        break;

    case 3:
        int multNum1, multNum2, multTotal;

        // Gather Input
        System.out.println("Please Enter The First Number To Multiply: ");
        multNum1 = input.nextInt();
        System.out.println("Please Enter The Second Number To Multiply: ");
        multNum2 = input.nextInt();

        // This will Multiply the Numbers
        multTotal = multNum1 * multNum2;

        //Display Final
        System.out.println("The Multiplied Numbers Are: " +multTotal);
        break;

    case 4: 
        //Definitions
        double divNum1, divNum2, divTotal;
        System.out.println("Enter Your Numerator ");
        divNum1 = input.nextInt();
        System.out.println("Enter Your Denominator ");
        divNum2 = input.nextInt();
        if(divNum2 == 0){
            System.out.println("Zero is Not divisable, please select a new denominator: ");
            divNum2 = input.nextInt();
        }
        divTotal = divNum1 / divNum2;
        System.out.println("Your divisor is: " +divTotal);
        break;

    case 5:
        double limL, limH, rand;
        System.out.println("Enter Your Low Limit: ");
        limL = input.nextInt();
        System.out.println("Enter Your High Limit ");
        limH = input.nextInt();

        //Equation to keep numbers within bounds
        rand = limL + (Math.random() * ((limH - limL) + 1));
        System.out.println("Given Your Limits, the Random Number will be: " +rand);
        break;

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

    }

    }

    }

   }
Comment

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

do{
  //Menu options
  System.out.print("6.) Exit
");
  System.out.print("
Enter Your Menu Choice: ");

  choice = input.nextInt();

  switch(choice){
        //Your cases from 1 to 6.
        default:
            System.out.println("Invalid menu choice; try again.");
            break;
   }
}while(choice != 6);
Comment

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

    //set choiceentry to -1, this will make it to enter while loop
     int choiceentry = -1

    while(choiceentry < 1 || choiceentry > 3){

            System.out.println("Enter "1", "2", "3" or "4"");
            if(scanchoice.hasNextInt())
            choiceentry = scanchoice.nextInt();

    }

     switch(choiceentry){
        case 1:
           //do logic
           break;
        case 2:
           //do logic
           break;
        case 3:
           //do logic
           break;
   }
Comment

PREVIOUS NEXT
Code Example
Java :: recursion of numbers in decending order in java 
Java :: Read File and Resource in JUnit Test into Stream 
Java :: Implementation of TreeMap Class in Java map 
Java :: iterative inorder traversal 
Java :: java array merge 
Java :: is the function same as method in java 
Java :: reverse arraylist java recursion 
Java :: difference between getcontext and getapplicationcontext in android 
Java :: java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider 
Java :: java swing border 
Java :: java hashmap remove by condition 
Java :: java list tostring 
Java :: can a java class have more than 108 constructors 
Java :: string to pojo java 
Java :: spring security antmatchers id 
Java :: get sum of array and return string 
Java :: Java if...else 
Java :: how to insert a 0 in an array java 
Java :: java do while loop 
Java :: arrondi java 
Java :: how to create a node in java 
Java :: remove element from char array java 
Java :: java record 
Java :: java switch tutorial 
Java :: get value from Spring application.properties 
Java :: get node inside node in of xml using java 
Java :: different constructiors in java and what they do explained 
Java :: what is encapsulation in java 
Java :: using super in java 
Java :: java replaceall single character 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =