b 没有返回 main 的值,为什么这是错误的?
b is not returning the value for main, why this is wrong?
我正在学习这个简单的代码,但我不明白为什么 b 不 return 除法的值。例如,我输入 10 和 2,它为我输出 2 而不是 5。
感谢您的宝贵时间!
import java.util.Scanner;
class Throw_Exception{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a = 0;
do{
System.out.println("Input number A and B: ");
a = input.nextInt();
int b = input.nextInt();
input.close();
div(a,b);
System.out.println("The division is " + b);
}while (a != -1 );
}
static int div(int a, int b) throws ArithmeticException{
if(b == 0){
throw new ArithmeticException("Division by Zero");
}else{
b = a/b;
return b;
}
}
}
您忽略了 div(a,b)
返回的结果。您需要将值分配给 b
:
b = div(a,b);
我正在学习这个简单的代码,但我不明白为什么 b 不 return 除法的值。例如,我输入 10 和 2,它为我输出 2 而不是 5。
感谢您的宝贵时间!
import java.util.Scanner;
class Throw_Exception{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a = 0;
do{
System.out.println("Input number A and B: ");
a = input.nextInt();
int b = input.nextInt();
input.close();
div(a,b);
System.out.println("The division is " + b);
}while (a != -1 );
}
static int div(int a, int b) throws ArithmeticException{
if(b == 0){
throw new ArithmeticException("Division by Zero");
}else{
b = a/b;
return b;
}
}
}
您忽略了 div(a,b)
返回的结果。您需要将值分配给 b
:
b = div(a,b);