我正在尝试处理 Java 作业。这就是它的要求:
编写一个名为
TestScores
. 类构造函数应该接受一个测试分数数组作为它的参数。该类应该有一个返回测试分数平均值的方法。如果数组中的测试分数为负数或大于 100,则该类应抛出IllegalArgumentException
. 证明。我需要一个名为TestScores
and的文件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。