2015-10-14 53 views
-2

我需要帮助来创建一个平均方法。我已经完成了一切。它需要计算并返回所有学生测试分数平均值的平均值。我只有返回声明。尽管如此,我却遇到了麻烦。这是我的代码到目前为止。第一个是课程类,第二个是学生类。我需要帮助来创建一个平均方法

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; 
    } 
} 
} 
+3

你真的需要学习循环。 –

+0

我不明白为什么这会降低选票。我的问题很清楚,我也尝试了其他程序 – TheUnicornMaster

+0

我不是downvoter,但你的问题太宽泛无法回答。至少这太长时间无法调试。 –

回答

1

我不认为这样的练习应该给初学者。它会(如可以在这里看到的)诱使他们产生令人厌烦和烦人的重复代码。编程语言已经被开发出来,使得编程变得简单,而不是脖子上的痛苦。

由于我们在这里有一个限制问题(无阵列),OTOH可以被视为一种激励措施,以制定一个没有“禁止”功能的策略。但是,我再次怀疑编程入门是否应该在如此早期的阶段深入研究这些细节问题。

这就是说,我提出了班级学生的改写。查看评论。

//禁止使用数组和列表。

public class Course { 
    private String course; 
    private Student s1, s2, s3, s4, s5; 
    private int studentcount = 0; 

    public Course (String name) { 
     course = name; 
    } 

方法传递addStudent使用“转移”已存储的学生简单的技巧在s1以腾出空间给下一个学生。尽管一些空值被不必要地复制,但是重复测试并不会更昂贵,并且肯定不太容易出错。

public Student addStudent(String first, String last, String home, String school){ 
     if(studentcount < 5){ 
      studentcount++; 
      s5 = s4; s4 = s3; s3 = s2; s2 = s1; 
      return s1 = new Student(first,last,home,school); 
     }  
     System.out.println("No more students allowed in the class"); 
     return null; 
    } 

方法卷使用另一个标准技巧来避免重复行。来自s1的数据必须为所有学生计数显示更大1,s2必须打印所有计数大于2等。应避免重复扩展字符串,因此应避免使用StringBuilder。

public String roll(){ 
     StringBuilder sb = new StringBuilder(); 
     if (studentcount >= 1) sb.append(s1.toString()).append("\n"); 
     if (studentcount >= 2) sb.append(s2.toString()).append("\n"); 
     if (studentcount >= 3) sb.append(s3.toString()).append("\n"); 
     if (studentcount >= 4) sb.append(s4.toString()).append("\n"); 
     if (studentcount >= 5) sb.append(s5.toString()).append("\n"); 
     return sb.toString(); 
    } 

average方法使用相同的技术,现在累积得分。请注意,转换为计算商的两倍。

public double average(){ 
     int scores = 0; 
     if (studentcount >= 1) 
      scores += s1.getTestScore(1) + s1.getTestScore(2) + s1.getTestScore(3); 
     if (studentcount >= 2) 
      scores += s2.getTestScore(1) + s2.getTestScore(2) + s2.getTestScore(3); 
     if (studentcount >= 3) 
      scores += s3.getTestScore(1) + s3.getTestScore(2) + s3.getTestScore(3); 
     if (studentcount >= 4) 
      scores += s4.getTestScore(1) + s4.getTestScore(2) + s4.getTestScore(3); 
     if (studentcount >= 5) 
      scores += s5.getTestScore(1) + s5.getTestScore(2) + s5.getTestScore(3); 
     return (double)scores/(studentcount*3); 
    } 
} 
+0

非常感谢您的帮助。如果我可以投多个票,我会。然而,我现在在公共学生addStudent中有一个小问题。在这一行中:return s1 = new Student(first,last,home,school);它表示不兼容的类型:java.lang.String不能转换为Address。家是突出显示。 – TheUnicornMaster