Question
Java Assignment, The problem I am having is that I can't get the menu to return after each user selection. The only time it does
Java Assignment, The problem I am having is that I can't get the menu to return after each user selection. The only time it does that is when a user makes an invalid choice. That part works correctly. It also needs to close if the person makes three incorrect selections in a row.
import java.util.Random;
import java.util.Scanner;
public class CalculatorWithMethods {
// scanner object creation
static Scanner input = new Scanner(System.in);
boolean mainloop = true;
public static void main(String[] args) {
//Declaring variables
double res;
int choice = (int) getMenuOption();
switch (choice) {
case 1:
{
//calling the method to get the operands
double operand1 = getOperand("What is the first number?");
double operand2 = getOperand("What is the second number?");
res = add(operand1, operand2);
System.out.println(operand1 + " + " + operand2 + " = " + res);
break;
}
case 2:
{
//calling the method to get the operands
double operand1 = getOperand("What is the first number?");
double operand2 = getOperand("What is the second number?");
res = subtract(operand1, operand2);
System.out.println(operand1 + " - " + operand2 + " = " + res);
break;
}
case 3:
{
//calling the method to get the operands
double operand1 = getOperand("What is the first number?");
double operand2 = getOperand("What is the second number?");
res = multiply(operand1, operand2);
System.out.println(operand1 + " * " + operand2 + " = " + res);
break;
}
case 4:
{
//calling the method to get the operands
double operand1 = getOperand("What is the first number?");
double operand2 = getOperand("What is the second number?");
if (operand2 == 0) {
System.out.println("NAN.");
} else {
//calling the method to get the operands
operand1 = getOperand("What is the first number?");
operand2 = getOperand("What is the second number?");
res = divide(operand1, operand2);
System.out.println(operand1 + " / " + operand2 + " = " + res);
}
break;
}
case 5:
{
double operand1 = getOperand("What is the lower limit ?");
double operand2 = getOperand("What is the upper limit ?");
res = random(operand1, operand2);
System.out.println("The Random Number is :" + res);
break;
}
}
}
//This method will perform the add operation
public static double add(double operand1, double operand2) {
return operand1 + operand2;
}
//This method will perform the subtract operation
public static double subtract(double operand1, double operand2) {
return operand1 - operand2;
}
//This method will perform the multiply operation
public static double multiply(double operand1, double operand2) {
return operand1 * operand2;
}
//This method will perform the division operation
public static double divide(double operand1, double operand2) {
return operand1 / operand2;
}
//This method returns the random number
public static double random(double x, double y) {
Random generator = new Random();
return generator.nextInt((int)(y - x) + 1) + x;
}
//This method will get the operands entered by the user
public static double getOperand(String str) {
System.out.print(str);
double num = input.nextDouble();
return num;
}
//This method will display the menu
public static double getMenuOption() {
double choice;
// user interaction until quit performed.
while (true) {
// displaying menu
System.out.println(" Menu 1. Add 2. Subtract 3. Multiply 4. Divide 5. Generate Random Number 6. Quit");
// ask user choice
System.out.println("What would you like to do?");
choice = input.nextInt();
if (choice < 1 || choice > 5) {
System.out.println("** Invalid Choice **");
continue;
} else {
return choice;
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started