简单的Java计算器
首先,这不是一个家庭作业问题。我正在练习我的java知识。我认为做到这一点的一个好方法是在没有帮助的情况下编写一个简单的程序。不幸的是,我的编译器告诉我错误,我不知道如何修复。在不改变太多逻辑和代码的情况下,有人可以指出我的一些错误在哪里吗?谢谢
import java.lang.*;
import java.util.*;
public class Calculator
{
private int solution;
private int x;
private int y;
private char operators;
public Calculator()
{
solution = 0;
Scanner operators = new Scanner(System.in);
Scanner operands = new Scanner(System.in);
}
public int addition(int x, int y)
{
return x + y;
}
public int subtraction(int x, int y)
{
return x - y;
}
public int multiplication(int x, int y)
{
return x * y;
}
public int division(int x, int y)
{
solution = x / y;
return solution;
}
public void main (String[] args)
{
System.out.println("What operation? ('+', '-', '*', '/')");
System.out.println("Insert 2 numbers to be subtracted");
System.out.println("operand 1: ");
x = operands;
System.out.println("operand 2: ");
y = operands.next();
switch(operators)
{
case('+'):
addition(operands);
operands.next();
break;
case('-'):
subtraction(operands);
operands.next();
break;
case('*'):
multiplication(operands);
operands.next();
break;
case('/'):
division(operands);
operands.next();
break;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这一切都很棒,但是你用什么程序来编写你的java呢?
也许您应该考虑使用像 Eclipse 这样的 IDE,因为它可以自动检测错误并添加导入。 (我不确定你的是否这样做)它还告诉你你的程序的问题是什么“英语”。
另外,考虑这个类可能是一种更简单、更简单的计算器方法:
}
作为一个习惯问题,与其这样做:
不如这样做:
这在这里可能没有多大区别,但是如果您正在运行一个更大的程序,导入整个 java.util 将大大减慢您的速度程序。
希望这有帮助!
This is all great, but what program are you using to write your java?
Maybe you should consider using an IDE like Eclipse, as it can detect errors automatically and also adds imports. (I'm not sure if yours does this) It also tells you what the problem with your program is 'in english'.
Also, consider this class as maybe an easier and less complicated way of doing a calculator:
}
And as a matter of habit, instead of doing:
it is better to do:
This probably doesn't make much of a difference here, but if you are running a much bigger program importing the whole of java.util will considerably slow down your program.
Hope this helps!
您的主要方法需要这样声明:
此外,您似乎只为所有算术方法(加法、减法等)提供一个参数,尽管它们需要两个。
不能用
addition(operands)
调用,即只有一个参数,并且参数的类型错误(该方法需要两个int
,你给它一个<代码>扫描仪)。所有这些方法都是如此。您需要从Scanner
中提取int
。您可以使用 Scanner.nextInt() 来做到这一点。Your main method needs to be declared like this:
Furthermore, it seems like you are only supplying one argument to your all your arithmetic methods (addition, subtraction etc), although they require two.
Can not be called with
addition(operands)
, that is only one argument, and the argument is of the wrong type (the method needs twoint
, you give it aScanner
). The same goes for all those methods. You need to extract theint
s from theScanner
. You can do that withScanner.nextInt()
.您要求用户输入整数,但您将语句
operands.next();
作为输入。尝试与变量和用户输入保持一致,因此将其更改为 operands.nextInt() 会有所帮助。You are asking for the user to type in integers, but you put the statement
operands.next();
as the input. Try to keep consistent with your variables and user input, so changing it tooperands.nextInt()
would help.作为提示,开始投掷通常不是一个好主意
into your program because it makes the program unnecessarily large and slow. All you need for this is to
If I'm correct, most if not everything in java.lang is already imported for you.
Just as a tip, it's generally not a good idea to start throwing
into your program because it makes the program unnecessarily large and slow. All you need for this is to
If I'm correct, most if not everything in java.lang is already imported for you.
这会起作用
This will work
操作数
和运算符
超出了main的范围。您在构造函数中声明局部变量,因此当您退出构造函数时,它们有资格进行 GC 并且消失。您有编译错误 - 其中 10 个。
operands
andoperators
are out of scope for main. You declare local variables in the constructor, so when you exit the ctor they're eligible for GC and gone.You have compilation errors - 10 of them.
另一个问题是,该行
尝试放置从 String。 html#next()" rel="nofollow noreferrer">
Scanner.next()
到声明为类型的变量
。y
中intScanner.nextInt( )
方法可用于尝试返回int
。Another issue is, the line
is attempting to place a
String
returned fromScanner.next()
into the a variabley
which is declared as a typeint
.The
Scanner.nextInt()
method can be used to attempt to return anint
.除了其他答案之外,您的 main() 方法必须是静态的才能成为程序入口点。在 main() 中,您需要构造自己的 Calculator 对象,并调用该对象的方法。
In addition to the other answers, your main() method must be static in order to be a program entry point. In main() you will need to construct your own Calculator object, and call methods on that.