Java中的数组运行方法

时间:2017-07-23 03:13:05

标签: java arrays

获得了我需要创建Student和Course类以及Driver类的经典作业。

我在课程课程中计算整体课程平均值时遇到了麻烦。一切正常,但是当我运行驱动程序时,我得到了' 0' 0回到班级平均水平。在IDE中,它告诉我' s'在我的s.getAverage();'在getClassAverage方法中无法解决,但我不确定为什么我在其他地方使用它就好了。我试图通过一系列学生,并将他们的所有平均值加在一起。

任何指针?我很感激你的时间!

Student.java

public class Student
{
private String firstName, lastName;
private int testScore1, testScore2, testScore3;
private int studentAverage;

//Constructors
public Student()
{
    firstName = "None";
    lastName = "None";
    testScore1 = 0;
    testScore2 = 0;
    testScore3 = 0;
    studentAverage = 0;
}

public Student(String first, String last, int score1, int score2, int score3)
{
    firstName = first;
    lastName = last;
    testScore1 = score1;
    testScore2 = score2;
    testScore3 = score3;
}

//Setter for test scores
public void setTestScore(int testNum, int score)
{
    if (testNum == 1)
        testScore1 = score;
    else
        if (testNum == 2)
            testScore2 = score;
        else
          if (testNum == 3)
            testScore3 = score;
          else
            throw new IllegalArgumentException(testNum + " is out of range");
    }
//Getter for test scores
public int getTestScore(int testNum2)
{
    if (testNum2 == 1)
        return testScore1;
    else
        if (testNum2 == 2)
            return testScore2;
        else
          if (testNum2 == 3)
            return testScore3;
          else
            throw new IllegalArgumentException(testNum2 + " is out of range");
    }

//Getter for student first name and last name
public String getFirstName()
{
    return firstName;
}

public String getLastName()
{
    return lastName;
}

//Calculates average for each student
public int getAverage()
{
    int studentAverage = (testScore1 + testScore2 + testScore3)/3;
    return studentAverage;
}
//Returns a description of this Student object
public String toString()
{
    String result;

    result = firstName + " " + lastName + "\n";
    result += "Test Score 1:\n" + testScore1 + "\n";
    result += "Test Score 2:\n" + testScore2 + "\n";
    result += "Test Score 3:\n" + testScore3 + "\n";
    result += "Average Test Score:\n" + studentAverage;

    return result;
}
}

Course.java

import java.util.ArrayList;

public class Course {
//What Course class knows
private String courseName;
private int classAverage;
private int studentAverage, sumAverages;
private ArrayList<Student> students;

public static int count = 0;

//Constructor
public Course(String courseName)
{
    this.courseName = courseName;

    students = new ArrayList<Student>();
}

//Add students
public void addStudent(Student s)
{
    students.add(s);
    ++count;
}

//Return average of all student test score averages
public int getClassAverage()
{
    for(Student s : students);
    {
        sumAverages += s.getAverage();
    }

    int classAverage = (int) sumAverages/students.size();
    return classAverage;
}


//Roll call - prints all students in the course
public void roll()
{
    System.out.println("Course: " + courseName);
    System.out.println("Number of Students: " + count);
    System.out.println("Students: ");

    for(Student s : students) 
    {
        System.out.println(s.getFirstName() + " " + s.getLastName());
    }
}

//Prints class average
public String toString()
{
    String result;
    result = "The class average is: "+ classAverage;
    return result;
}

}

驱动程序

public class CourseDriver {

public static void main(String[] args) {
    //Create a new course
    Course course1 = new Course("CSC 140");

    //Create and add several students to course
    course1.addStudent( new Student("Sarah", "Mauer", 93, 82, 67) );
    course1.addStudent( new Student("Andrew", "Kagan", 85, 80, 75) );
    course1.addStudent( new Student("Carly", "Sanseverino", 95, 83, 80) );

    //Prints roll
    course1.roll();

    System.out.println(course1);
}

}

1 个答案:

答案 0 :(得分:0)

for循环后面的分号结束了&#39; s的范围。删除它,一切都应该好。