如何在for循环中处理异常而不重启程序

时间:2016-09-20 00:57:52

标签: java while-loop exception-handling try-catch java.util.scanner

我正在尝试计算N个非负整数的平均值。我要求用户输入他们想要计算平均值的数量。然后让他们逐个输入数字。我也在使用try并在do语句中捕获,以便在输入错误的值时再次提示用户。

如果任何输入的N编号不是数字(int),如何重新提示用户。? 我重新提示用户重新启动。我很欣赏一些方向!

编辑:我使用相同的do while循环来捕获输入是否为非数字。我现在得到的是无限数量的打印输出错误并重新提示用户输入。我必须停止程序运行。

编辑:问题是由于扫描仪未被清除。通过在嵌套的do-while循环中添加第二个扫描程序来修复。

public class Average {

    public static void main(String[] args) {

        boolean flag = false;

        do {
        try {
            System.out.println("Enter the number of integers you want to "
                               + "calculate the average of: ");

            Scanner input = new Scanner(System.in);
            int value = input.nextInt();
            int[] numbers = new int[value];
            int sum = 0; 

            if( value == 0 ) {
                throw new ArithmeticException();
            }
            boolean flag2 = false;
            do{
                try{

                    Scanner sc = new Scanner(System.in);
                    for( int i = 0; i < numbers.length; i++) {
                    System.out.println("Enter the " + (i+1) + " number: ");
                    numbers[i] = sc.nextInt();
                    sum += numbers[i];
                    flag2 = true;

                    }    
                } catch ( InputMismatchException e ) {
                    System.err.println("Cannot calculate average of non-numeric values.");
                } catch ( NumberFormatException e) {
                    System.out.println("Cannot calculate average of non-numeric values.!!");
                }
            } while (!flag2);


            double average = (double) sum / numbers.length;
            System.out.println("The numbers you have entered are:                " + Arrays.toString(numbers));
            System.out.println("The sum of the numbers is:                       " + sum);
            System.out.println("The number to divide by is:                      " + numbers.length);
            System.out.println("The average of the numbers you have entered is:  " + average);
            flag = true;

            } catch (InputMismatchException e) {
                System.err.println("Input cannot be non-numeric values");


            } catch (ArithmeticException e) {
                System.err.println("Input can only be positive integers");


            } catch (NegativeArraySizeException e) {
                System.err.println("Input can only be positive integers");
            }
        } while (!flag);
    }  
}

2 个答案:

答案 0 :(得分:0)

当用户输入非号码时,该程序应该投掷NumberFormatException。您需要处理该案例以及其余的catch块

    } catch (NumberFormatExceptione) {
        System.err.println("Input can only be only numbers");
    }

答案 1 :(得分:0)

只有在收到有效输入时,您才需要这样的内容来递增计数器。当收到错误的输入时,请记得清除扫描仪缓冲区。

while (counter < numbers.length) {
    try {
        numbers[counter] = input.nextInt();
        counter++;
    } catch (InputMismatchException e) {
        input.nextLine(); //This is to clear the scanner which is now holding the incorrect input

    } catch (NumberFormatException e) {
        input.nextLine(); 
    }
}
System.out.println(Arrays.toString(numbers));