获得最佳学生并计算平均分

时间:2018-01-19 01:34:53

标签: java arrays class methods

我们获得了Java类Student,类Randomizer和Class Classroom。 我们被告知要填写getTopStudent和getAverageScore的方法。

下面我列出了Student,Classroom和ClassroomTester的课程。

问题:当我运行它时,编译错误。

我不知道为什么这是错的...我没有忘记分号,我不确定他们的意思是“非法表达”

public class Student
{
    private static final int NUM_EXAMS = 4;
    
    private String firstName;
    private String lastName;
    private int gradeLevel;
    private double gpa;
    
    private int[] exams;
    private int numExamsTaken;

  
    public Student(String fName, String lName, int grade)
    {
        firstName = fName;
        lastName = lName;
        gradeLevel = grade;
        exams = new int[NUM_EXAMS];
        numExamsTaken = 0;
    }
    
    public double getAverageScore()
    {
        int sum = 0;
        for(int i = 0; i < exams.length;i++)
        {
            sum+=exams[i];
        }
        return (double)sum/numExamsTaken;
    }
    
    public String getName()
    {
        return firstName + " " + lastName;
    }
    
    public void addExamScore(int score)
    {
        exams[numExamsTaken] = score;
        numExamsTaken++;
    }
    
    // This is a setter method to set the GPA for the Student.
    public void setGPA(double theGPA)
    {
        gpa = theGPA;
    }
    
 
    public String toString()
    {
        return firstName + " " + lastName + " is in grade: " + gradeLevel;
    }
}

public class Classroom
{
    Student[] students;
    int numStudentsAdded;
    
    public Classroom(int numStudents)
    {
        students = new Student[numStudents];
    
    public Student getTopStudent()
    {
        double max = students[0].getAverageScore();
        String topstudent = students[0].getName();
        for (int i = 0; i < students.length; i++) 
        {
           if (students[i].getAverageScore() > max) 
           {
               max = students[i].getAverageScore();
               topstudent = students[i];
           }
        }  
        return topstudent;
    }
    public void addStudent(Student s)
    {
        students[numStudentsAdded] = s;
        numStudentsAdded++;
    }
    
    public void printStudents()
    {
        for(int i = 0; i < numStudentsAdded; i++)
        {
            System.out.println(students[i]);
        }
    }
}

public class ClassroomTester extends ConsoleProgram
{
    public void run()
    {
        Classroom c = new Classroom(2);
        
        Student ada = new Student("Ada", "Lovelace", 12);
        ada.addExamScore(44);
        ada.addExamScore(65);
        ada.addExamScore(77);

        Student alan = new Student("Alan", "Turing", 11);
        alan.addExamScore(38);
        alan.addExamScore(24);
        alan.addExamScore(31);

        // add students to classroom
        c.addStudent(ada);
        c.addStudent(alan);
        c.printStudents();
        
        Student topStudent = c.getTopStudent();
        System.out.println(topStudent);
    }
}

2 个答案:

答案 0 :(得分:0)

将Classroom的构造函数更改为

--------------------
| My Account       |
|------------------|
| Some Actions     |
|------------------|
| Info             |
|------------------|
| Log Out          |
--------------------

你在Classroom的构造函数中缺少右括号。

答案 1 :(得分:-1)

您错过了结束{

public Classroom(int numStudents)
    {
        students = new Student[numStudents];