所以我试图做一个简单的程序,它有一个模拟大学课程的数组。您可以将学生添加到课程中(称为IOOP),获取姓名,地位,测验分数,测验平均数,期末考试平均数等。该数组将学生作为其项目,其中一种方法称为printFinalExamDetails。我的教授希望我们不仅能够显示整个班级的平均期末考试成绩(我已经编码),还要显示新生,大二,大三学生和老年人的平均期末考试分数。我的代码看起来像现在这种权利:如何返回字符串数组列表中某些项目的数量?
public void printFinalExamDetails () {
double sum, counter; sum = 0;
for (Student x : students)
{
sum = sum + x.getFinalExamScore();
counter = students.size();
System.out.println (x.getFirstName() + " " + x.getLastName() + ": " + x.getFinalExamScore());
System.out.println ("The average final exam score is: " + sum/counter + ".");
String standing = x.computeStanding();
if(standing.equals("freshman"))
System.out.println ("The average final exam score for the freshmen is: " + sum/counter + ".");
if(standing.equals("sophomore"))
System.out.println("The average final exam score for the sophomore is: " + sum/counter + ".");
}
}
现在恐怕连连使用计数器将使用类的全尺寸,而不仅仅是大一/ sophs /大三/大四了,如果我用学生.size(),它将打印数组中的所有学生,而不仅仅是新生。任何人都可以给我一些关于如何编码的提示吗?谢谢!
请正确格式化您的代码。 – jrd1 2013-03-11 05:37:39
'counter'的用途是什么?这仅仅是一个班级人数的数字吗?为什么不把它叫做classSize? – Thunderforge 2013-03-11 05:45:03
你需要明白,班级的平均分数是所有学生的总分数/总数。的学生'。二年级的平均成绩是所有二年级学生的总分/总数。二年级学生。新鲜者的平均得分是所有新鲜者的总得分/总数。新鲜'。尝试基于此更新您的代码。 – Kshitij 2013-03-11 05:56:05