7

我正在尝试处理 Java 作业。这就是它的要求:

编写一个名为TestScores. 类构造函数应该接受一个测试分数数组作为它的参数。该类应该有一个返回测试分数平均值的方法。如果数组中的测试分数为负数或大于 100,则该类应抛出IllegalArgumentException. 证明。我需要一个名为TestScoresand的文件TestScoresDemo

这就是我到目前为止所拥有的。我知道其中一些是错误的,我需要帮助来修复它:

class TestScores {
    public static void checkscore(int s) {
        if (s<0) throw new IllegalArgumentException("Error: score is negative.");
        else if (s>100) throw new IllegalArgumentException("Error Score is higher then 100");
        else if (s>89)throw new IllegalArgumentException("Your grade is an A");
        else if (s>79 && s<90)throw new IllegalArgumentException("Your grade is an B");
        else if (s>69 && s<80)throw new IllegalArgumentException("Your grade is an C");
        else if (s>59 && s<70)throw new IllegalArgumentException("Your grade is an D");
        else if (s<60)throw new IllegalArgumentException("Your grade is an F");

        {
            int sum = 0; //all elements together
            for (int i = 0; i < a.length; i++)
                sum += a[i];
        }
        return sum / a.length;
    }
}

class TestScoresDemo {
    public static void main(String[] args) {
        int score = 0;
        Scanner scanner = new Scanner(System.in);
        System.out.print(" Enter a Grade number: ");
        String input = scanner.nextLine();
        score = Integer.parseInt(input);
        TestScores.checkscore(score);
        System.out.print("Test score average is" + sum);
    }
}

我知道作业需要一个try声明,因为在我的书中,这就是我在IllegalArgumentException. 谁能帮我?我使用 Eclipse 作为 IDE。

4

3 回答 3

4

您的TestScores类应该有两个成员:一个接受分数数组的构造函数和一个返回分数平均值的方法。如果测试分数超出范围,则分配并不完全清楚其中哪些应该抛出IllegalArgumentException,但我会将其设为构造函数(因为这就是有论点的内容)。

public class TestScores {
    public TestScores(int[] scores) throws IllegalArgumentException {
        // test the scores for validity and throw an exception if appropriate
        // otherwise stash the scores in a field for later use
    }

    public float getAverageScore() {
        // compute the average score and return it
    }
}

你的TestScoresDemo班级走在了正确的轨道上。它首先需要将一组分数收集到一个数组中。然后它应该构造一个TestScores对象。这是需要在try/catch块内的内容,因为它可以引发异常。然后你只需要打电话getAverageScore()并对结果做一些事情。

于 2012-04-27T03:11:25.177 回答
0

异常是用来定义在应用程序的正常流程中不正确的东西。当调用 checkScore 方法并找到范围之外(0 到 100 之间)的任何参数时,您必须抛出 IllegalArgumentException。

你的班级应该有这样的结构:

public class TestScore {

    private int scores[]; //With setters and getters.

    public TestScore(int scores[]) {
        //Here, you set the scores array to the one on this class.
    }

    public int getAverage() {
        //You do the average here, and since you need to iterate over the 
        //array to sum each value, you can check the value and if it's not
        //ok you throw the IllegalArgumentException. No throws keyword
        //required since this kind of exception (like NullPointerException
        //and many others) are unchecked exceptions, meaning they can be 
        //thrown by a method and it does not need to specify them.
    }

}

测试类应该创建一个带有 int 数组的 TestScore 对象作为其构造函数的参数。然后你创建一个 testAverageScore 方法,上面有 try-catch 语句,因为它需要调用 getAverage 方法。

希望有帮助。祝你好运!。

编辑: IllegalArgumentException 是未经检查的异常。

于 2012-04-27T03:23:35.883 回答
-1
public class TestScores {
private final int[] scores;

public TestScores(int[] scores) {
    this.scores = scores;
}

public int getAverage() {
    int sum = 0;

    if(scores.length == 0) {
        return 0;
    }

    for(int score: scores) {
        if(score < 0 || score > 100) {
            throw new IllegalArgumentException("Score is not valid!");
        }
        sum += score;
    }
    return sum/scores.length;
}

}

于 2012-04-27T03:12:13.630 回答