【问题标题】:Trouble correctly handling exceptions无法正确处理异常
【发布时间】:2016-08-21 03:12:03
【问题描述】:

我是一名初级 Java 程序员,正在编写一个计算一组正整数的平均值的程序。首先,它向用户询问要输入的整数数量。然后,它从用户那里收集整数并输出计算的平均值。我在处理异常时遇到了麻烦。当用户尝试输入要平均的负数时,异常会正确显示,但它不能正确地继续 for 循环以收集适当数量的数字。例如,下面是一个示例输出:

Please enter the number of integers to be averaged: 5
Enter a number: 1
Enter a number: 2
Enter a number: -3
NegativeIntegerException: N must be a positive integer.
Enter a number: 3
Enter a number: 4
The average is: 0.0

它正确地抛出了异常,但没有继续填充 5 个整数。只有其他 4 个有效。

下面是代码:

import java.util.*;

class NegativeIntegerException extends Exception {

    public NegativeIntegerException()
    {
        super("N must be a positive integer.");
    }
}


public class intAverage {

    public static void main(String[] args)
    {
        int N = 0; //number of integers to be averaged
        int[] numbers = null; //array to hold integers
        int sum = 0;
        int newInt;
        double average;
        Scanner keyboard = new Scanner(System.in);
        int x = 1; //for do-while loop #1
        int z = 1; //for do-while loop #2

        do {
        try {
            System.out.print("Please enter the number of integers to be averaged: ");
            N = keyboard.nextInt();
            numbers = new int[N]; //setting size of array
            x = 2;
        } 
        catch (Exception e) {
            System.out.println("N must be a positive integer");
        }
        } while (x == 1);

        do {
            for(int i = 0; i < N; i++) //collecting the integers
            {
                System.out.print("Enter a number: ");
                newInt = keyboard.nextInt();
                if (newInt < 0) {
                    try {
                        throw new NegativeIntegerException();
                    }
                    catch(NegativeIntegerException e) {
                        System.out.println(e);
                    }
                }
                newInt = numbers[i];
            }
            z = 2;
        } while (z == 1);


        for(int y = 0; y < N; y++) //calculate average
        {
            sum = sum + numbers[y];
        }
        average = sum / N;

        System.out.println("The average is: " + average);

    }

}

【问题讨论】:

  • 我想你的意思是说...numbers[i] = newInt 不是newInt = numbers[i]

标签: java


【解决方案1】:

当你打印出异常时,从 i 中减去 1,然后继续。这基本上会重新开始迭代。

do {
    for(int i = 0; i < N; i++) //collecting the integers
    {
        System.out.print("Enter a number: ");
        newInt = keyboard.nextInt();
        if (newInt < 0) {
            try {
                throw new NegativeIntegerException();
            }
            catch(NegativeIntegerException e) {
                System.out.println(e);
                // Ignore this input
                i--;
                continue;
            }
        }
        numbers[i] = newInt;
    }
    z = 2;
} while (z == 1);

请注意,您不需要为此工作抛出异常 - 您可以简单地说:

do {
    for(int i = 0; i < N; i++) //collecting the integers
    {
        System.out.print("Enter a number: ");
        newInt = keyboard.nextInt();
        if (newInt < 0) {
            System.out.println("NegativeIntegerException: N must be a positive integer");
            // Ignore this input
            i--;
            continue;
        }
        numbers[i] = newInt;
    }
    z = 2;
} while (z == 1);

这也更具可读性。

【讨论】:

  • 啊,我明白了。我进行了更改,它现在可以正确处理负数。谢谢你。我想知道为什么如果我输入带小数点的分数、字母或数字,为什么它会使程序陷入无限循环?我猜它与 do-while 循环有关,但我不确定什么是处理类型不匹配异常的有效方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 2018-11-07
相关资源
最近更新 更多