Java,以数组为参数的构造函数和来自 Driver 的用户输入

Java, Constructor w/ array as an argument & user input from Driver

我是 java 的新手,我似乎在一些小事上受阻,尤其是涉及带参数的方法时。

这是一项 class 作业,主要是关于异常处理,我相信我是正确的。我知道出了什么问题,只是不知道如何解决。

让我解释一下。赋值要求我们设置一个构造函数,该构造函数将双精度数组作为参数(用于测试分数)并在分数超出 0-100 范围时抛出 IllegalArgumentException。该程序正在正确计算测试分数的平均值,但我的问题是(我认为)实际上没有任何东西被传递到构造函数中,以便构造函数可以查找异常。我相信它只是 运行 通过构造函数但没有将任何东西传递给它,这就是为什么当我传入负数时不会抛出异常的原因。

我不知道我做错了什么,非常感谢任何帮助!

/**Write a class named TestScores. The class constructor should 
 * accept an array of test scores as its argument. The class should 
 * have a method that returns the average of the test scores. If any
 * test score in the array is negative or greater than 100, the class
 * should throw an IllegalArgumentException. Demonstrate the class in a program.
*/

public class TestScores {

   private double testScores[];
   ScoresDemo TD = new ScoresDemo();

    public TestScores(double scores[]) {
        testScores = scores;
        try {
            for(int i = 0; i < testScores.length; i++) {
                if(scores[i] < 0 || scores[i] > 100) {
                    throw new IllegalArgumentException("Test scores must be between 0 and 100");
                }
                else {
                    testScores[i] = scores[i];
                }
            }
        }catch(IllegalFormatException ex) {
            System.out.println(ex);
        }
   }

    public double averageScores() {
        double average = 0;
        int count = testScores.length;
        int sum = 0;
            for(int i = 0; i < testScores.length; i++) {
                sum += testScores[i];
            }
         average = sum / count;
         return average;
    } 
}

"DRIVER" CLASS

public class ScoresDemo {

    public static void main(String[] args) {
        double testScores[] = new double [5];
        TestScores scores = new TestScores(testScores);
        Scanner scan = new Scanner(System.in);

        for(int i = 0; i < testScores.length; i++) {
            System.out.println("Enter some test scores: ");
            testScores[i] = scan.nextDouble();
        }
        System.out.println("The average of the test scores is " + scores.averageScores());
    }
}

只有当它仍然是一个空数组时才传递 testScores。需要稍后传递或更新(重新传递)。

不会抛出异常,因为数组中没有值 "invalid";也就是说,由于数组中的每个值都被分配了默认值 0.0,所以不可能抛出异常。

但是,问题仍然存在,即您传递的是一个包含零的数组,并期望在其上发生大量数学运算,但事实并非如此。在看到结果之前,您需要传入一个包含实际值的初始化数组。

这只是 TestScores 填充数组后实例化一个实例的问题。

public static void main(String[] args) {
        double testScores[] = new double [5];
        Scanner scan = new Scanner(System.in);

        for(int i = 0; i < testScores.length; i++) {
            System.out.println("Enter some test scores: ");
            testScores[i] = scan.nextDouble();
        }
        TestScores scores = new TestScores(testScores);
        System.out.println("The average of the test scores is " + scores.averageScores());
    }

问题是您的构造函数在您填充数组时没有重新计算。它被评估一次,当它被调用时。不幸的是,你在第一次创建数组时调用它,此时所有分数都是默认值 (0d)。

您需要做的是首先填充数组,然后将其传递给 TestScores 构造函数。即,换掉for循环和构造函数的顺序:

public class ScoresDemo {

    public static void main(String[] args) {
        double testScores[] = new double [5];
        Scanner scan = new Scanner(System.in);

        for(int i = 0; i < testScores.length; i++) {
            System.out.println("Enter some test scores: ");
            testScores[i] = scan.nextDouble();
        }
        // Now that we have a populated testScores array, call the constructor:
        TestScores scores = new TestScores(testScores);
        System.out.println("The average of the test scores is " + scores.averageScores());
    }
}