0
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 或更高。如果不是,它应该继续遍历异常。

4

5 回答 5

2

你不需要一个ArrayIndexOutOfBoundException

for (int i = 0; i < size; i++) // < --- use (size) instead of constant 20 value
{
    System.out.println("Good.");
}  

finally block加上 done = true;

}  
finally
{
    done = true;
}  

这将防止无限循环抛出异常。
还要在程序开头添加验证:

int size = new Integer(input).intValue(); 
if (size >= 20)  
{
    throw new IllegalArgumentException("Number more then 19");  
}
于 2013-09-15T17:05:03.487 回答
0

在后面添加以下行int size = new Integer(input).intValue();

if(size<20)
throw new ArrayIndexOutOfBoundsException("size is not equal to or greater than 20");

如果 Size 小于 20,它将抛出异常。并且不需要编写这些trycatch方法。

希望这对您有所帮助...

于 2013-09-15T19:49:02.910 回答
0

回答这个问题的最简单方法是浏览代码并弄清楚到底发生了什么。为简单起见,假设您输入了三个。

import java.util.*;

public class ArrayExample {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        boolean done = false;  //ok so we set done to false
        while (!done) { //first time through, we are not done, so we enter the loop

            try {
                System.out.println("Please enter the size of the array:"); //you ask for a number 
                String input = keyboard.next(); //we put in '3'

                int size = new Integer(input).intValue(); //convert the input to an int
                int numbers[] = new int[size]; //we set the size to '3'

                for (int i = 0; i < 20; i++) { //we enter the loop, i is 0 so we continue
                    numbers[i] = i; //we set the value of idx 0 to 0
                    done = true; //we set done to true so we will exit the while loop
                    System.out.println("Good."); //we print good
                    //we increment i to 1
                }
                //EXPANDED LOOP FOR CLARITY
                for (int i = 0; i < 20; i++) { //second time, i is now 1
                    numbers[i] = i; //we set the value of idx 1 to 1
                    done = true; //we set done to true again (kind of redundant)
                    System.out.println("Good."); //we print good
                    //we increment i to 2
                }
                for (int i = 0; i < 20; i++) { //third time, i is now 2
                    numbers[i] = i; //we set the value of idx 2 to 2
                    done = true; //we set done to true again (still kind of redundant)
                    System.out.println("Good."); //we print good
                    we increment i to 3
                }
                for (int i = 0; i < 20; i++) { //fourth time, i is now 3
                    numbers[i] = i; //at this point we should throw an ArrayIndexOutOfBoundsException, so go to that catch statement
                    done = true; 
                    System.out.println("Good.");
                }

            } catch (NumberFormatException ex) {
                System.out.println("NumberFormatException Error. Please enter a integer.");
            } catch (ArrayIndexOutOfBoundsException ex) { //so here we catch the AIOB exception
                System.out.println("ArrayIndexOutOfBoundsException Error. Please enter 20 or higher."); //we print out an error, then continue
            } catch (NegativeArraySizeException ex) {
                System.out.println("NegativeArraySizeException Error. Please do not enter a negative.");
            }
            //here at the end of the block, we go back to check the while condition
            //we set done to true, so the while will fail and we will exit the program
        }
    }
}

鉴于此,您的输出应如下所示:

Good
Good
Good
ArrayIndexOutOfBoundsException Error. Please enter 20 or higher.

就这样。该程序将完成。因为您还没有发布输出,所以很难进一步调试。

于 2013-09-15T18:39:53.323 回答
0

试试下面的代码,你需要检查给定数字是否小于 20 并抛出异常,那部分代码丢失

import java.util.*;
public class ArrayExample {
private static Scanner keyboard;
public static void main(String[] args) throws Exception {
    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];
            if(size <20){
                throw new Exception("Number less than 20");
            }

            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.");
        }
    }
}
}
于 2013-09-15T17:08:16.820 回答
0

我不完全理解,但是我让这个程序要做的是,如果数字小于 20,它会抛出 ArrayIndexOutOfBoundsException,但如果它是 20 或更大,它将打印出“Good” for how many ever整数大小的倍数。我还将 for 循环更改为增强的 for 循环:

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];

            if(numbers.length >= 20) {
                for (int i : numbers) {
                    numbers[i] = i;
                    done = true;
                    System.out.println("Good.");
                }
            } else {
                throw new ArrayIndexOutOfBoundsException("ArrayIndexOutOfBoundsException Error. Please enter 20 or higher.");
            }
        } catch (NumberFormatException ex) {
            System.out.println("NumberFormatException Error. Please enter a integer.");
        } catch (NegativeArraySizeException ex) {
            System.out.println("NegativeArraySizeException Error. Please do not enter a negative.");
        }
    }
}
于 2013-09-15T17:10:47.983 回答