我必须用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;
}
}
发生的情况是程序一遍又一遍地提示用户输入相同的操作,直到您手动停止程序运行。我尝试将整个事情放入不同类型的循环中,但没有任何改变。
最佳答案
由于执行操作的代码位于循环 ( while (menuOption >= 1 && menuOption <= 6)
) 内,因此程序将继续循环执行最后选择的操作。
您需要一个还包含 getMenuOption()
的循环方法,以便用户可以选择其他操作。
为此,您可以只用 1 个循环来处理所有事情,而不是使用 2 个单独的循环(还请记住,您可以在 default
内使用 switch
情况)。
由于这看起来像是家庭作业,我不会为您提供完整的解决方案,但如果您有其他具体疑问,请告诉我们。
关于java - 如何打破情况并重复程序直到用户选择退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42052915/