Java System.out.println打印20次

时间:2013-09-15 17:01:57

标签: java

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或更高版本,否则它应抛出异常。但是,它打印“好”。如果我输入一个低于20的数字。所以,如果我输入19,它将打印“好”。 19次。如果我输入3,它将打印“好”。 3次。我只想要打印“好”。如果我输入20或更高。如果不是,它应该继续循环遍历例外。

5 个答案:

答案 0 :(得分: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");  
}

答案 1 :(得分: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.");
        }
    }
}
}

答案 2 :(得分:0)

我完全不明白,但是我有这个程序要做的是如果数字小于20,它将抛出ArrayIndexOutOfBoundsException,但如果它是20或更大,它将打印出“Good”for整数大小有多少倍。我还将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.");
        }
    }
}

答案 3 :(得分: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.

就是这样。该计划将完成。由于您尚未发布输出,因此很难进一步调试。

答案 4 :(得分: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方法。

希望这有助于你...