好的,我已经编写了这段代码并多次查看,看看能不能找到我的错误。不幸的是,我找不到它,我对自己感到沮丧。我决定在线发布我的代码。这是带有方法的实际Calculator类。输入类(不在此帖子上)获取用户输入并将其作为字符串返回。有人可以帮帮我吗?谢谢兄弟们。
public class Calculate extends UserInput{
int num1;
int num2;
int returnValue;
String operation;
String userInput1;
String userInput2;
public void calculate() {
userInput1 = getUserInput("Enter a number: ");
num1 = Integer.parseInt(userInput1); //Changing userInput to int
operation = getUserInput("Enter an operation (+,-,*, or /): ");
userInput2 = getUserInput("Enter a second number: "); //Changing second userInput to int
num2 = Integer.parseInt(userInput2);
if(operation == "+") {
addition(num1, num2);
} else if (operation == "-") {
subtraction(num1, num2);
} else if (operation == "*") {
multiplication(num1, num2);
} else if (operation == "/") {
division(num1, num2);
}
System.out.println("Result: " + returnValue);
}
public double addition(int x, int y) {
returnValue = x + y;
return returnValue;
}
public double subtraction(int x, int y) {
returnValue = x - y;
return returnValue;
}
public double multiplication(int x, int y) {
returnValue = x * y;
return returnValue;
}
public double division(int x, int y) {
returnValue = x / y;
return returnValue;
}
}
答案 0 :(得分:1)
这是我解决问题的方法!谢谢社区!
if(operation .equals("+")) {
returnValue = addition(num1, num2);
} else if (operation .equals("-")) {
returnValue = subtraction(num1, num2);
} else if (operation .equals("*")) {
returnValue = multiplication(num1, num2);
} else if (operation .equals("/")) {
returnValue = division(num1, num2);
}
答案 1 :(得分:0)
if(operation.equals("+")) {
addition(num1, num2);
} else if (operation.equals("-")) {
subtraction(num1, num2);
} else if (operation.equals("*")) {
multiplication(num1, num2);
} else if (operation.equals("/")) {
division(num1, num2);
}