プログラム全体を何度も繰り返すのではなく、メソッドを呼び出す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;
}
}
何が起こっているかは、プログラムの実行を手動で停止するまで、ユーザーに同じ操作の入力を繰り返し求めていることです。私は、さまざまな種類のループに全体を入れてみましたが、何も変わっていません。
あなたの必要性はあまり明確ではありません。 – davidxxx
は 'switch'を含む' while-loop'を削除し、 'switch'のままにしておきます。また、ユーザーが別の操作を実行できるようにするには、 'main'の中にすべてを含めます。 –