gpt4 book ai didi

Java简单计算器,了解s​​witch和方法兼容性

转载 作者:行者123 更新时间:2023-12-01 17:07:09 24 4
gpt4 key购买 nike

我在暑期类有一项作业是制作一个简单的计算器,但我在一些事情上遇到了麻烦。

开头有一个菜单,询问用户想要做什么,其中一个选项是退出,其中一个选项是对用户输入进行平方。

我遇到的问题是我无法找到用开关退出程序的方法,所以我把它从开关中取出并做了一个条件,这样就可以了。所以,我想这不是什么大问题,除非有人愿意向我展示如何将它包含在交换机中。我现在遇到的问题是对用户输入进行平方。问题是程序询问用户他们想要做什么,然后要求他们输入两个操作数。然而,我只希望它询问一个操作数。因此,我将其放入条件中,它只要求一个操作数,但它似乎不再使用平方方法并计算结果。

非常感谢任何帮助!!

import java.util.Scanner;

public class Driver {

public static void main(String[] args) {

Menu m = new Menu();
m.getMenu();

}
}



class Menu {
public void getMenu() {
Scanner scan = new Scanner(System.in);

System.out.println("Welcome to 00 Calculator menu, this calculator has the following options:");
System.out.println("1 - Add " + "\n" + "2 - Subtract" + "\n"
+ "3 - Multiply" + "\n" + "4 - Divide" + "\n"
+ "5 - Square" + "\n" + "6 - Modulus" + "\n" + "0 - Quit" );
int optionInt = scan.nextInt();

if (optionInt == 0) {
System.out.println("Goodbye!");
return;

}else if (optionInt == 5) {
System.out.println("Enter the first number :");
double num1 = scan.nextDouble();
}else {
System.out.println("Enter the first number :");
double num1 = scan.nextDouble();
System.out.println("Enter the second number :");
double num2 = scan.nextDouble();

double result;
Action option = new Action();


switch (optionInt) {
case 1:
result = option.addValues(num1, num2);
option.displayResult(result);
break;
case 2:
result = option.subtractValues(num1, num2);
option.displayResult(result);
break;
case 3:
result = option.multiplyValues(num1, num2);
option.displayResult(result);
break;
case 4:
result = option.divideValues(num1, num2);
option.displayResult(result);
break;
case 5:
result = option.squareValues(num1, num2);
option.displayResult(result);
break;
case 6:
result = option.modulusValues(num1, num2);
option.displayResult(result);
break;
default:
System.out.println("You entered an incorrect option. Goodbye.");
}
}
}
}


class Action {

public double divideValues(double d1, double d2) {
double result = d1 / d2;
return result;
}

public double multiplyValues(double d1, double d2) {
double result = d1 + d2;
return result;
}

public double subtractValues(double d1, double d2) {
double result = d1 - d2;
return result;
}

public double addValues(double d1, double d2) {
double result = d1 + d2;
return result;
}

public double squareValues(double d1, double d2) {
double result = Math.pow(d1, 2);
return result;
}

public double modulusValues(double d1, double d2) {
double result = d1 % d2;
return result;
}

public void displayResult(double result) {
System.out.println("The answer is " + result);
}

}

最佳答案

由于您的要求很复杂,退出选项最好保留在原来的位置...

现在,如果您不选择0,则有两种可能性,您需要根据选项需要询问一个或两个值。

在这种情况下,如果 optionInt != 5,那么您需要两个值,但是,您始终需要一个,因此您始终可以要求,例如...

if (optionInt == 0) {
System.out.println("Goodbye!");
return;
} else {
System.out.println("Enter the first number :");
double num1 = scan.nextDouble();
double num2 = 0;
if (optionInt != 5) {
System.out.println("Enter the second number :");
num2 = scan.nextDouble();
}
double result;
Action option = new Action();


switch (optionInt) {
case 1:
result = option.addValues(num1, num2);
option.displayResult(result);
break;
case 2:
result = option.subtractValues(num1, num2);
option.displayResult(result);
break;
case 3:
result = option.multiplyValues(num1, num2);
option.displayResult(result);
break;
case 4:
result = option.divideValues(num1, num2);
option.displayResult(result);
break;
case 5:
result = option.squareValues(num1); // You will need to update your Action class
option.displayResult(result);
break;
case 6:
result = option.modulusValues(num1, num2);
option.displayResult(result);
break;
default:
System.out.println("You entered an incorrect option. Goodbye.");
}

}

读完代码后,您似乎甚至不需要检查退出选项,因为一旦您以任何方式完成操作,您的方法就会退出,因此,您可以简单地执行类似的操作。 ..

if (optionInt != 0) {  
// Process other options...
}

//... method will exit of it's own accord, you don't need to do anything...

已更新

另一种方法可能是编写一个方法来提示用户输入您需要的值,例如,当您需要它们时......

public double getDouble(String prompt) {
System.out.println(prompt);
Scanner scan = new Scanner(System.in);
double value = scan.nextDouble();
scan.nextLine();
return value;
}

public void getMenu() {
Scanner scan = new Scanner(System.in);

System.out.println("Welcome to 00 Calculator menu, this calculator has the following options:");
System.out.println("1 - Add " + "\n" + "2 - Subtract" + "\n"
+ "3 - Multiply" + "\n" + "4 - Divide" + "\n"
+ "5 - Square" + "\n" + "6 - Modulus" + "\n" + "0 - Quit");
int optionInt = scan.nextInt();
scan.nextLine();

double result;
Action option = new Action();
switch (optionInt) {
case 0:
System.out.println("Good bye!");
break;
case 1:
result = option.addValues(
getDouble("Enter the first number :"),
getDouble("Enter the second number :")
);
option.displayResult(result);
break;
case 2:
result = option.subtractValues(
getDouble("Enter the first number :"),
getDouble("Enter the second number :")
);
option.displayResult(result);
break;
case 3:
result = option.multiplyValues(
getDouble("Enter the first number :"),
getDouble("Enter the second number :")
);
option.displayResult(result);
break;
case 4:
result = option.divideValues(
getDouble("Enter the first number :"),
getDouble("Enter the second number :")
);
option.displayResult(result);
break;
case 5:
result = option.squareValues(
getDouble("Enter a number :"),
);
option.displayResult(result);
break;
case 6:
result = option.modulusValues(
getDouble("Enter the first number :"),
getDouble("Enter the second number :")
);
option.displayResult(result);
break;
default:
System.out.println("You entered an incorrect option. Goodbye.");
}
}

关于Java简单计算器,了解s​​witch和方法兼容性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25072867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com