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:错误:没有“ if”的“ else” 其他{
Calculator.java:33:错误:没有“ if”的“ else” 其他{
还是个初学者,所以如果您想添加一些技巧,我将不胜感激。
答案 0 :(得分:0)
您没有正确关闭方法“ divide”和“ modulo”中的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;
}
}