0

这是代码,据我了解,它应该可以工作。此外,当我调试我的程序时,似乎该数组正在填充相同方法正在创建的对象。但是,当我尝试打印它时,它仍然显示“null”返回 agian,就像“return”不起作用一样。为什么会这样?

public class Course {

String courseName;
String teacherName;
int gradeAv;
static int counter;
static int maxNumOfStudents;
Student studentsArray[] = new Student[5];
int numOfStudents = studentsArray.length;

public Course() {

}

public Course(String courseName) {

    this();
    this.courseName = courseName;
}

public Course(String courseName, String teacherName) {

    this(courseName);
    this.courseName = courseName;
    this.teacherName = teacherName;
}

public Student[] addStudent(String name, int age, int grade) {

    for (int i = 0; i < 5; i++) {

        studentsArray[i] = new Student(name, age, grade);

        age += 10;
        grade += 10;
    }
    return studentsArray;
}

public void printStudentArray(Student studentArray[]) {

    studentArray = this.studentsArray;

    for (int i = 0; i < studentsArray.length; i++) {

        System.out.println(studentsArray[i]);
    }
}

public int gradeAv() {

    for (int i = 0; i < studentsArray.length; i++) {

        int temp = 0;

        if (studentsArray[i].grade > temp) {
            gradeAv = temp;
            System.out.println(gradeAv);
        }
    }
    return gradeAv;
}

public void printCourse() {

    System.out.println("Course: ");
    System.out.println("Course Name: " + courseName + ". "
            + "Teacher's Name: " + teacherName + ". "
            + "Number Of Students: " + numOfStudents + ". ");

}
   }

这是我的主要课程:

public class MainClass {

public static void main(String[] args) {

    Student stud = new Student();
    Course cour = new Course("Java", "Ronni");

    stud.addStudent("Joe", 23, 100);

    stud.printStudent();
    System.out.println();
    stud.printCourse();

    System.out.println();

    cour.printStudentArray(cour.studentsArray);
    System.out.println();

// cour.gradeAv();

}

}

4

2 回答 2

3

Student stud = new Student();
Course cour = new Course("Java", "Ronni");

stud.addStudent("Joe", 23, 100);

stud.addStudent("Joe", 23, 100);不会将任何学生添加到您的课程中。 stud是一个完全不同的类的实例,Student的实现Student不在您发布的代码中。

调用addStudent方法cour而不是 forstud

cour.addStudent("Joe", 23, 100);

反而


我还想补充一点,您的代码中有很多奇怪的元素,其中一些人在评论中提出了。把它们都列出来会很乏味。我会仔细检查一下,以确保您没有做多余的事情。

于 2013-03-15T16:09:39.060 回答
1

试试这个例如:

课程类别:

import java.util.ArrayList;
import java.util.List;

public class Course {

private List<Student> students = new ArrayList<Student>();
private String teacherName;
private String subjectName;

public Course(String subjectName, String teacherName) {
    this.subjectName = subjectName;
    this.teacherName = teacherName;
}

public void addStudent(Student student) {
    students.add(student);
}

public float getAverageGrade() {
    float grade = 0;
    for (Student student : students) {
        grade += student.getGrade();
    }
    return grade / students.size();
}

public void printCourse() {
    System.out.println("Course "+subjectName+" taught by "+teacherName);
    System.out.println("Students:");
    printStudents();
    System.out.println("Aberage grade: "+getAverageGrade());
}

public void printStudents() {
    for (Student student : students) {
        System.out.println(student.getName()+"\t age "+student.getAge()+" \t grade "+student.getGrade());
    }
}
}

学生班级:

public class Student {

private String name;
private int age;
private int grade;

public Student(String name, int age, int grade) {
    this.grade = grade;
    this.age = age;
    this.grade = grade;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public int getGrade() {
    return grade;
}

public void setGrade(int grade) {
    this.grade = grade;
}


}
于 2013-03-15T16:22:28.467 回答