我需要帮助来创建一个平均方法。我已经完成了一切。它需要计算并返回所有学生测试分数平均值的平均值。我只有返回声明。尽管如此,我却遇到了麻烦。这是我的代码到目前为止。第一个是课程类,第二个是学生类。我需要帮助来创建一个平均方法
public class Course
{
private String course;
private Student s1, s2, s3, s4, s5;
private int studentcount = 0;
public Course (String name)
{
course = name;
}
public Student addStudent(String first, String last, Address home, Address school)
{
if (studentcount == 0){
s1 = new Student(first,last,home,school);
studentcount++;
return s1;
}
if (studentcount == 1) {
s2 = new Student(first,last,home,school);
studentcount++;
return s2;
}
else if (studentcount == 2){
s3 = new Student(first,last,home,school);
studentcount++;
return s3;
}
else if (studentcount == 3){
s4 = new Student(first,last,home,school);
studentcount++;
return s4;
}
else if (studentcount == 4) {
s5 = new Student(first,last,home,school);
studentcount++;
return s5;
}
else { //the course can only have five students
System.out.println("No More students allowed in the class");
return null;
}
}
public double average() //returns the average
{
return (s1.average() + s1.average() + s1.average() + s1.average() + s1.average())/5.0;
}
public String roll() //returns student info for each number of students
{
String results = "";
if (studentcount == 1){
results += s1.toString() +"n";
return results;
}
if (studentcount == 2) {
results += s1.toString() +"n";
results += s2.toString() +"n";
return results;
}
else if (studentcount == 3){
results += s1.toString() +"n";
results += s2.toString() +"n";
results += s3.toString() +"n";
return results;
}
else if (studentcount == 4){
results += s1.toString() +"n";
results += s2.toString() +"n";
results += s3.toString() +"n";
results += s4.toString() +"n";
return results;
}
else if (studentcount == 5) {
results += s1.toString() +"n";
results += s2.toString() +"n";
results += s3.toString() +"n";
results += s4.toString() +"n";
results += s5.toString() +"n";
return results;
}
else{
return null;
}
}
}
public class Student
{
private String firstName, lastName;
private Address homeAddress, schoolAddress;
private int test1, test2, test3;
//-----------------------------------------------------------------
// Sets up this Student object with the specified initial values.
//-----------------------------------------------------------------
public Student (String first, String last, Address home, Address school)
{
firstName = first;
lastName = last;
homeAddress = home;
schoolAddress = school;
test1 = 0;
test2 = 0;
test3 = 0;
}
//-----------------------------------------------------------------
// Returns this Student object as a string.
//-----------------------------------------------------------------
public String toString()
{
String result;
result = firstName + " " + lastName + "\n";
result += "Home Address:\n" + homeAddress + "\n";
result += "School Address:\n" + schoolAddress;
return result;
}
public void setTestScore(int t, int g) //sets the test score
{
if (t == 1)
{
test1 = g;
}
else if (t == 2)
{
test2 = g;
}
else if (t == 3)
{
test3 = g;
}
}
public int getTestScore (int t) //returns the test score
{
if (t == 1)
{
return test1;
}
else if (t == 2)
{
return test2;
}
else
{
return test3;
}
}
}
你真的需要学习循环。 –
我不明白为什么这会降低选票。我的问题很清楚,我也尝试了其他程序 – TheUnicornMaster
我不是downvoter,但你的问题太宽泛无法回答。至少这太长时间无法调试。 –