私にとって、これらのコード
static int faktorial (int n) throws ArithmeticException {
if ((n < 0) || (n > 31)) {
throw new ArithmeticException();
}
if (n > 1) {
return n * faktorial(n - 1);
}
else {
return 1;
}
}
なしの同じコード
throws ArithmeticException
次のコードで使用する場合は、同じことを行います。
public static void main(String[] args) {
try {
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Insert integer number: ");
n = sc.nextInt();
System.out.println(n + "! = " + faktorial(n));
}
catch (InputMismatchException e) {
System.out.println("Not an integer number!");
e. printStackTrace();
}
catch (RuntimeException e) {
System.out.println("Number is too big!");
e. printStackTrace();
}
}
を使用する場合、誰かが私を説明できますか
throws ArithmeticException
私のコードにはいくつかの利点があります。
また、keyword を使用した良い例をいくつか紹介しthrows
ます。どうもありがとうございました!