-->
当前位置:首页 > 题库

函数题:Java异常处理

Luz3年前 (2022-11-10)题库625
设有一个整数数组a[], a有10个元素,其值依次为0到9。

从键盘输入整数i的值,求a[i]的倒数。

注意处理各种异常。发生异常后,根据不同的异常,输出警告。

提示:

需要考虑InputMismatchException、ArrayIndexOutOfBoundsException、ArithmeticException等多种异常。
### 裁判测试程序样例:
c++
import java.util.Scanner;
import java.util.InputMismatchException;

public class Main {
public static void main(String[] args) {
int[] a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

/* 请在这里填写答案 */
}
}




### 输入格式:

先输入一个整数n,表示有n组数据。

此后有n行,每行有一个整数i。

### 输出格式

正常情况下,输出1/a[i]的值(整数形式)。

如果发生InputMismatchException异常,输出“catch a InputMismatchException”。

如果发生ArrayIndexOutOfBoundsException异常,输出“catch a ArrayIndexOutOfBoundsException”。

如果发生ArithmeticException异常,输出“catch a ArithmeticException”。

### 输入样例:

in
4
1
10
0
a


### 输出样例:

out
1
catch a ArrayIndexOutOfBoundsException
catch a ArithmeticException
catch a InputMismatchException







答案:若无答案欢迎评论