Issue
I am pretty dang new to Java, but being familiar with some other programming languages, I know the basic lay out of a lot of it. One thing I am struggling with is looping menus, specifically a main menu.
I have tirelessly researched methods of it, but none seem to apply or work to my program. I'm assuming its something silly and small that I'm missing in my more-so basic program.
Check it below, thanks for any tips.
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();
}
while(choice >7);
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();
sum = adNumf + adNuml;
System.out.print("The Sum Of Those Numbers is: " +sum);
break;
case 2:
int subNum1, subNum2, sum2;
System.out.println("\nPlease 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;
}
// Bad Menu Option Direct
if (choice > 6 || choice < 1){
System.out.println("This is not a valid Menu Option! Please Select Another.");
do{
choice = input.nextInt();
}
while(choice < 7 );
}
// End bad menu option
}
}
Solution
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();
sum = adNumf + adNuml;
System.out.print("The Sum Of Those Numbers is: " +sum);
break;
case 2:
int subNum1, subNum2, sum2;
System.out.println("\nPlease 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;
}
}
}
}
Answered By - Anoop LL Answer Checked By - Mildred Charles (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.