CoreJava/exp9/ExceptionTest.java

27 lines
684 B
Java
Raw Normal View History

2024-05-09 09:10:51 +08:00
package exp9;
import java.util.Scanner;
public class ExceptionTest {
public static void main(String[] args) {
int[] nums = new int[5];
int size=0;
Scanner scan = new Scanner(System.in);
while (true) {
try {
String str = scan.nextLine();
nums[size] = Integer.parseInt(str);
size++;
} catch (NumberFormatException e) {
System.out.println("不接受非整数字符!");
} catch (ArrayIndexOutOfBoundsException e){
System.out.println("数组已满!");
break;
}
}
scan.close();
}
}