我必须在java中创建一个简单的计算器来调用方法,而不是一遍又一遍地重复整个程序。我的所有方法都有效,它允许用户根据需要做出错误的选择,直到做出正确的选择。我遇到的问题是,在操作完成并得出答案后,它不会突破案例。
package menuDrivenCalculator;
import java.util.Scanner;
public class MenuDrivenCalculator {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
int menuOption = getMenuOption();
while (menuOption < 1 || menuOption > 6) {
System.out.println("I'm sorry, " + menuOption + " is not a valid choice. Please try again.");
menuOption = getMenuOption();
if (menuOption >= 1 && menuOption <= 6) {
break;
}
}
while (menuOption >= 1 && menuOption <= 6) {
switch (menuOption) {
case 1:
System.out.print("What is the first number? ");
double operand1 = getOperand();
System.out.print("What is the second number?");
double operand2 = getOperand();
double add = add(operand1, operand2);
System.out.println("Your answer is: " + add);
break;
case 2:
System.out.print("What is the first number? ");
operand1 = getOperand();
System.out.print("What is the second number?");
operand2 = getOperand();
double subtract = subtract(operand1, operand2);
System.out.println("Your answer is: " + subtract);
break;
case 3:
System.out.print("What is the first number? ");
operand1 = getOperand();
System.out.print("What is the second number?");
operand2 = getOperand();
double multiply = multiply(operand1, operand2);
System.out.println("Your answer is: " + multiply);
break;
case 4:
System.out.print("What is the first number? ");
operand1 = getOperand();
System.out.print("What is the second number?");
operand2 = getOperand();
double divide = divide(operand1, operand2);
System.out.println("Your answer is: " + divide);
break;
case 5:
System.out.print("What is the lower limit? ");
operand1 = getOperand();
System.out.print("What is the upper limit?");
operand2 = getOperand();
double random = random(operand1, operand2);
System.out.println("Your answer is: " + random);
break;
case 6:
System.out.println("Goodbye!");
return;
}
}
}
public static int getMenuOption() {
System.out.println("Menu");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Generate a random number");
System.out.println("6. Quit\n");
System.out.print("What would you like to do? ");
int menuOption = input.nextInt();
return menuOption;
}
public static double getOperand() {
double operand = input.nextDouble();
return operand;
}
public static double add(double operand1, double operand2) {
double add = (operand1 + operand2);
return add;
}
public static double subtract(double operand1, double operand2) {
double subtract = (operand1 - operand2);
return subtract;
}
public static double multiply(double operand1, double operand2) {
double multiply = (operand1 * operand2);
return multiply;
}
public static double divide(double operand1, double operand2) {
double divide = 0;
if (operand2 == 0) {
divide = Double.NaN;
} else if (operand2 != 0) {
divide = (operand1 / operand2);
}
return divide;
}
public static double random(double operand1, double operand2) {
double random = Math.random() * operand2 + operand1;
return random;
}
}
正在发生的事情是程序会一次又一次地提示用户输入相同的操作,直到您手动停止程序运行。我尝试将整个东西放在不同类型的循环中,没有任何改变。
答案 0 :(得分:1)
由于执行操作的代码位于循环(while (menuOption >= 1 && menuOption <= 6)
)内,程序将继续循环上次选择的操作。
您需要一个包含getMenuOption()
方法的循环,以便用户可以选择其他操作。
为了做到这一点,你可以只用1来处理所有事情(请记住你也可以使用default
内的switch
案例),而不是有2个单独的循环。
由于它似乎是家庭作业,我不会给你完整的解决方案,但如果你有其他具体的疑问,请告诉我们。
答案 1 :(得分:0)
如果你不想重复,为什么把switch语句放在while循环中? 将“while”替换为代码中的“if”