Java BlueJ 计算器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18908534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BlueJ Calculator
提问by Ian Wilkins
I need to create a calculator that can add, subtract, multiply, divide, absolute value, and round. This is what I have so far
我需要创建一个可以加、减、乘、除、绝对值和舍入的计算器。这是我到目前为止
import java.util.Scanner;
public class Calculator
{
public static void main(String[] args)
{
}
public static int add(int num1, int num2)
{
return num1 + num2;
}
public static int subtract(int num1, int num2)
{
return num1 - num2;
}
public static int multiply(int num1, int num2)
{
return num1 * num2;
}
public static double divide(double num1, Double num2)
{
return num1 / num2;
}
public static double absoluteValue(double num1)
{
return;
if (num1 < 0)
{
return = num1 * -1;
}
else
{
num1;
}
}
public static double round(double num1)
{
return;
if (num1 + 0.5 >= (int) num1 + 1
{
int num2 = (int) num1 + 1
return = num2;
}
else
{
return = (int) num1
}
}
}
It will not compile correctly, and I do not know if it will function. What do I need to do to fix it?
它不会正确编译,我不知道它是否会起作用。我需要做什么来修复它?
回答by raladdin
You have errors in your code. In the two last functions, the first line returns immediately, when it should return a double or an int (you were also missing some parenthesis). Try this:
您的代码中有错误。在最后两个函数中,第一行立即返回,当它应该返回 double 或 int 时(您还缺少一些括号)。尝试这个:
public static double absoluteValue(double num1)
{
double res = 0;
if (num1 < 0) res = -num1;
else res = num1;
return res;
}
public static int round(double num1)
{
int res = 0;
if (num1 + 0.5 >= ((int) num1) + 1) res = ((int) num1) + 1;
else res = ((int) num1);
return res;
}
Note that return is a keyword in Java, not a variable.
请注意, return 是 Java 中的关键字,而不是变量。
You should definitely check out this tutorial : http://www.java-made-easy.com/java-methods.html
你一定要看看这个教程:http: //www.java-made-easy.com/java-methods.html
Also, your main doesn't call any methods so executing this program will not do anything, you need to add input/output interaction with the terminal if you want to simulate a calculator!
另外,你的 main 没有调用任何方法,所以执行这个程序不会做任何事情,如果你想模拟一个计算器,你需要添加与终端的输入/输出交互!
Cheers!
干杯!
回答by ailsa JOHN
public class calculator { private int num1; private int num2; private int result;
公共类计算器 { 私有 int num1; 私人 int num2; 私有整数结果;
public calculator(int newNum1, int newNum2) {
num1 = newNum1;
num2 = newNum2;
}
public int getNum1() {
return num1;
}
public int getNum2() {
return num2;
}
public int add(int newNum1, int newNum2) {
result = newNum1 + newNum2;
return result;
}
public int subtract(int newNum1, int newNum2) {
result = newNum1 - newNum2;
return result;
}
public int divide(int newNum1, int newNum2){
result = newNum1 / newNum2;
return result;
}
public int multiply(int newNum1, int newNum2) {
result = newNum1 * newNum2;
return result;
}
public void printResult() {
System.out.println("The result is " + result + ".");
}
}