【发布时间】:2020-04-16 07:13:24
【问题描述】:
我在一个Java程序中有如下方法:
public void Div(int a, int b){
//exception in order to check if the second argument is 0
try{
int div1 = (a / b);
double div = div1;
System.out.println("The division of the two numbers is: " + div);
}
catch(ArithmeticException e){
System.out.println("You can't divide a number by 0");
}
这仅适用于分子大于分母(例如 8/2)的情况。如果分子小于分母,我会得到 0.0(例如 2/8)的结果。
我该怎么做才能让它发挥作用?
【问题讨论】:
-
使用其他数据类型然后 int。 int 只是整数,没有小数。将 a、b 和 div1 更改为 float 或 double