JAVA 编写程序 计算a=4c/b 处理当b=0时的情况

来源:百度知道 编辑:UC知道 时间:2024/06/25 11:44:51

int a ,b,c
if (b==0){
System.out.println("分母不能为零!");
}
else{
a= 4c/b;
}

大家误解了LZ的意思了,LZ说的是处理当b=0时的情况
所以抛出一个异常就可以了也不需要判断
public class Div {
public int operator(int a,int b){
int result = 0;
try {
result = a/b;
} catch (ArithmeticException e) {
System.err.println("除数不能为0");
return 0;
}
return result;
}
public static void main(String[] args) {
Div div = new Div();
div.operator(3, 0);
}
}
////////////////////
结果:除数不能为0

写一个if语句判断一下啊

觉得3楼、4楼跟楼主意思差不多