import java.util.*;
public class ArrayExample {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
boolean done = false;
while (!done) {
try {
System.out.println("Please enter the size of the array:");
String input = keyboard.next();
int size = new Integer(input).intValue();
int numbers[] = new int[size];
for (int i = 0; i < 20; i++) {
numbers[i] = i;
done = true;
System.out.println("Good.");
}
} catch (NumberFormatException ex) {
System.out.println("NumberFormatException Error. Please enter a integer.");
} catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("ArrayIndexOutOfBoundsException Error. Please enter 20 or higher.");
} catch (NegativeArraySizeException ex) {
System.out.println("NegativeArraySizeException Error. Please do not enter a negative.");
}
}
}
}
当我运行此程序时,它无法正常运行。除非我输入 INTEGER 20 或更高,否则它应该抛出异常。但是,它会打印“Good”。如果我输入的数字低于 20。所以如果我输入 19,它将打印“Good”。19 次。如果我输入 3,它将打印“Good”。3次。我只希望它打印“好”。如果我输入 20 或更高。如果不是,它应该继续遍历异常。