gpt4 book ai didi

java - 计算器程序不断得出答案 "0"?

转载 作者:行者123 更新时间:2023-12-01 11:34:46 25 4
gpt4 key购买 nike

好的,我已经编写了这段代码并检查了很多次,看看是否能找到我的错误。不幸的是,我找不到它,并对自己感到沮丧。我决定将我的代码发布到网上。这是带有方法的实际 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;
}

}

最佳答案

这是我的问题的解决方法!感谢社区!

    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);
}

关于java - 计算器程序不断得出答案 "0"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30114941/

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