我需要幫助來創建一個平均方法。我已經完成了一切。它需要計算並返回所有學生測試分數平均值的平均值。我只有返回聲明。儘管如此,我卻遇到了麻煩。這是我的代碼到目前爲止。第一個是課程類,第二個是學生類。我需要幫助來創建一個平均方法
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,但你的問題太寬泛無法回答。至少這太長時間無法調試。 –