【发布时间】:2013-12-31 21:25:51
【问题描述】:
您好,我前两天对 java 产生了兴趣,在看一些教程时,在看到一个超级简单的加法计算示例完成后,我决定制作一个计算器。我想知道我哪里出错了。请帮助我对它超级陌生。(如果有影响的话,我也使用 eclipse idk)
import java.util.Scanner;
public class calculator {
public static void main(String args[]){
Scanner operation = new Scanner(System.in);
Scanner number = new Scanner(System.in);
int x;
int y;
int problem = multiplication, division, addition, subtraction;
int answer = answerM, answerD, answerA, answerS;
System.out.println("enter first number: ");
x = number.nextInt();
System.out.println("enter operator: ");
signs(); = operation.nextInt();
System.out.println("enter second number: ");
y = number.nextInt();
System.out.println(answer);
if (problem == signs()){
answerM = x * y;
}else{
if (problem = signs()){
answerD = x / y;
}else{
if (problem == signs()){
answerA = x + y;
}else{
if (problem == signs()){
answerS = x - y;
}
}
}
}
}
private static int signs() {
multiplication = "*";
division = "/";
addition = "+";
subtraction = "-";
return 0;
}
}
错误,错误
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
multiplication cannot be resolved to a variable
answerM cannot be resolved to a variable
Syntax error on token "=", delete this token
answerM cannot be resolved to a variable
Type mismatch: cannot convert from int to boolean
at calculator.main(calculator.java:10)
【问题讨论】:
-
在开始编写计算器之前,请花一些时间阅读语言参考。像
int problem = multiplication, division, addition, subtraction;这样的语句表明你需要一些bit阅读。 -
@devnull,如果之前声明了
multiplication,它将起作用;) -
问题太多了。开始从文档中学习或购买一本好书。
-
“一边看教程”。我会推荐阅读一些书籍。 YouTube 教程遗漏了很多细节。
-
伟大的 Java 教科书“Java 编程入门”,作者 Daniel Liang。我相信最新版本是第 9 版 :)
标签: java calculator