缺少第二个值内存计算器
Missing second value memory calculator
我的程序运行了,但是当我需要它来计算我需要它做的事情(加、减、乘、除)时,它只是输入我输入的值,而不会执行我告诉它的操作为此,它只是再次显示菜单。我如何修复它以便它可以执行它需要执行的功能并循环回到菜单以执行另一个功能? (例如,我想将 2 和 2 相加,然后反过来将 3 和 2 相乘)
public static void main(String[] args) {
Testt calc = new Testt();
calc.getCurrentValue();
displayMenu();
}
public static int displayMenu() {
Scanner input = new Scanner(System.in);
Testt calc = new Testt();
int choice;
do {
System.out.println("Hello, welcome to the menu");
System.out.println("Select one of the following items from the 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) Clear ");
System.out.println("6.Quit");
System.out.println ("Please choice an option from the menu:");
choice = input.nextInt();
if (choice > 6 || choice < 1) {
System.out.println("Sorry," + choice + " was not an option");
return displayMenu();
}
} while (choice > 6 && choice < 1);
if (choice == 5) {
calc.clear();
return 0;
} else if (choice == 6) {
System.out.println("Goodbye! ");
System.exit(0);
}
System.out.println("What is the second number? ");
double operand2 = input.nextDouble();
switch (choice) {
case 1:
calc.add(operand2);
break;
case 2:
calc.subtract(operand2);
break;
case 3:
calc.multiply(operand2);
break;
case 4:
calc.divide(operand2);
break;
}
return choice;
}
public static double getOperand(String prompt) {
return 0;
}
private double currentValue;
public double getCurrentValue() {
System.out.println("The current value is " + currentValue);
return 0;
}
public void add(double operand2) {
currentValue = currentValue + operand2;
getCurrentValue();
}
public void subtract(double operand2) {
currentValue = operand2 - currentValue;
getCurrentValue();
}
public void multiply(double operand2) {
currentValue = operand2 * currentValue;
getCurrentValue();
}
public void divide(double operand2) {
if (operand2 == 0) {
System.out.println("Sorry, you can not divide by 0");
}
currentValue = operand2 / currentValue;
getCurrentValue();
}
public void clear() {
currentValue = 0;
getCurrentValue();
}
从 getCurrentValue()
开始,您总是 returning 0。
您需要 return 当前值。
另外,如果你想循环回到同样的事情,你可以把所有这些都放在 while(true)
循环中。像这样:
public static int displayMenu() {
Scanner input = new Scanner(System.in);
Demo calc = new Demo();
int choice;
System.out.println("Hello, welcome to the menu");
System.out.println("Select one of the following items from the 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) Clear ");
System.out.println("6.Quit");
while(true){
System.out.println ("Please choice an option from the menu:");
choice = input.nextInt();
if (choice > 6 || choice < 1) {
System.out.println("Sorry," + choice + " was not an option");
continue;
}
if (choice == 5) {
calc.clear();
return 0;
} else if (choice == 6) {
System.out.println("Goodbye! ");
System.exit(0);
}
System.out.println("What is the second number? ");
double operand2 = input.nextDouble();
switch (choice) {
case 1:
calc.add(operand2);
break;
case 2:
calc.subtract(operand2);
break;
case 3:
calc.multiply(operand2);
break;
case 4:
calc.divide(operand2);
break;
}
}
}
我的程序运行了,但是当我需要它来计算我需要它做的事情(加、减、乘、除)时,它只是输入我输入的值,而不会执行我告诉它的操作为此,它只是再次显示菜单。我如何修复它以便它可以执行它需要执行的功能并循环回到菜单以执行另一个功能? (例如,我想将 2 和 2 相加,然后反过来将 3 和 2 相乘)
public static void main(String[] args) {
Testt calc = new Testt();
calc.getCurrentValue();
displayMenu();
}
public static int displayMenu() {
Scanner input = new Scanner(System.in);
Testt calc = new Testt();
int choice;
do {
System.out.println("Hello, welcome to the menu");
System.out.println("Select one of the following items from the 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) Clear ");
System.out.println("6.Quit");
System.out.println ("Please choice an option from the menu:");
choice = input.nextInt();
if (choice > 6 || choice < 1) {
System.out.println("Sorry," + choice + " was not an option");
return displayMenu();
}
} while (choice > 6 && choice < 1);
if (choice == 5) {
calc.clear();
return 0;
} else if (choice == 6) {
System.out.println("Goodbye! ");
System.exit(0);
}
System.out.println("What is the second number? ");
double operand2 = input.nextDouble();
switch (choice) {
case 1:
calc.add(operand2);
break;
case 2:
calc.subtract(operand2);
break;
case 3:
calc.multiply(operand2);
break;
case 4:
calc.divide(operand2);
break;
}
return choice;
}
public static double getOperand(String prompt) {
return 0;
}
private double currentValue;
public double getCurrentValue() {
System.out.println("The current value is " + currentValue);
return 0;
}
public void add(double operand2) {
currentValue = currentValue + operand2;
getCurrentValue();
}
public void subtract(double operand2) {
currentValue = operand2 - currentValue;
getCurrentValue();
}
public void multiply(double operand2) {
currentValue = operand2 * currentValue;
getCurrentValue();
}
public void divide(double operand2) {
if (operand2 == 0) {
System.out.println("Sorry, you can not divide by 0");
}
currentValue = operand2 / currentValue;
getCurrentValue();
}
public void clear() {
currentValue = 0;
getCurrentValue();
}
从 getCurrentValue()
开始,您总是 returning 0。
您需要 return 当前值。
另外,如果你想循环回到同样的事情,你可以把所有这些都放在 while(true)
循环中。像这样:
public static int displayMenu() {
Scanner input = new Scanner(System.in);
Demo calc = new Demo();
int choice;
System.out.println("Hello, welcome to the menu");
System.out.println("Select one of the following items from the 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) Clear ");
System.out.println("6.Quit");
while(true){
System.out.println ("Please choice an option from the menu:");
choice = input.nextInt();
if (choice > 6 || choice < 1) {
System.out.println("Sorry," + choice + " was not an option");
continue;
}
if (choice == 5) {
calc.clear();
return 0;
} else if (choice == 6) {
System.out.println("Goodbye! ");
System.exit(0);
}
System.out.println("What is the second number? ");
double operand2 = input.nextDouble();
switch (choice) {
case 1:
calc.add(operand2);
break;
case 2:
calc.subtract(operand2);
break;
case 3:
calc.multiply(operand2);
break;
case 4:
calc.divide(operand2);
break;
}
}
}