Java中数组的运行方法
得到了一个经典的作业,我需要创建学生和课程类,以及一个司机类 我在计算课程中的总平均成绩时遇到了问题。一切都正常,但是当我运行驱动程序时,我得到了“0”作为类平均值。在IDE中,它告诉我我的s中的“s”。getAverage();'在getClassAverage中,方法无法解决,但我不确定为什么,因为我在其他地方使用它很好。我试着通过一系列学生,把他们的平均数加起来 有什么建议吗?谢谢你的时间 Student.javaJava中数组的运行方法,java,arrays,Java,Arrays,得到了一个经典的作业,我需要创建学生和课程类,以及一个司机类 我在计算课程中的总平均成绩时遇到了问题。一切都正常,但是当我运行驱动程序时,我得到了“0”作为类平均值。在IDE中,它告诉我我的s中的“s”。getAverage();'在getClassAverage中,方法无法解决,但我不确定为什么,因为我在其他地方使用它很好。我试着通过一系列学生,把他们的平均数加起来 有什么建议吗?谢谢你的时间 Student.java public class Student { private String
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;
}
}
for循环后面的分号结束了“s”的作用域。去掉这个,一切都会好起来。去掉这里的分号:
for(Student s:students)代码>。您从未调用过getClassAverage(),那么为什么希望它不是零呢?请尝试返回“Average”+getClassAverage()代码>。换句话说,当您对计算变量使用方法时,不需要该字段。您的代码中有两个错误:第一:删除循环后的分号,以避免IDE问题:for(Student s:students){summerages+=s.getAverage();}第二:为了避免0平均值:您声明了一个新的classAverage值并为其赋值,您应该为对象classAverage赋值:this.classAverage=(int)sumAverages/students.size();错误太多——这意味着您使用了错误的编码方式。在尝试添加更多代码之前,应该持续测试并立即修复所有错误。
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);
}
}