public class Calculator { Calculator() { } public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } public int multiply(int a, int b) { return a * b; } public int divide(int a, int b) { return a/b; if(b == 0) { System.out.println("Error! Dividing by zero is not allowed."); return 0; else { return a/b; } } } public int modulo(int a, int b) { if(b == 0) { System.out.println("Error! Dividing by zero is not allowed."); return 0; else { return a % b; } } } public static void main(String[] args) { Calculator myCalculator = new Calculator(); System.out.println(myCalculator.add(5, 7)); System.out.println(myCalculator.subtract(12, 34)); }}出现的错误:Calculator.java:23: 错误:'else' 没有 'if' else {Calculator.java:33: 错误:'else' 没有 'if' else {仍然是初学者,所以如果你想添加一些技巧,我会很感激。
1 回答
千巷猫影
TA贡献1829条经验 获得超7个赞
您没有在“除法”和“模数”方法中正确关闭句子 IF。例子:
public int divide(int a, int b) {
return a/b;
if(b == 0) {
System.out.println("Error! Dividing by zero is not allowed.");
return 0;
}else {
return a/b;
}
}
添加回答
举报
0/150
提交
取消