添加和删​​除学生的逻辑错误[Java]

时间:2016-04-24 01:41:39

标签: java class oop object

对于这个程序,我必须添加和删除两个课程的学生,这两个课程使用两个类(面向对象的编程)。到目前为止,这是我的代码:

public class Course {
private String courseName;
private String[] students = new String[100];
private int numberOfStudents;

public Course(String courseName) {
    this.courseName = courseName;
}

public void addStudent(String student) {
    students[numberOfStudents] = student;
    numberOfStudents++;
}

public String[] getStudents() {
    return students;
}

public int getNumberOfStudents() {
    return numberOfStudents;
}

public String getCourseName() {
    return courseName;
}

public void dropStudent(String student) {
    // set up a variable for the for loop
    int indexOfStudentToDrop = -1;

    // set up a for loop, with two if statements that drop students accordingly
    for (int i = 0; i < numberOfStudents; i++) {
        // if this if statement is true, drop student
        if(students[i].equalsIgnoreCase(student)) {

            indexOfStudentToDrop = i;
        }
        // if this if statement is true, DON'T drop student and increase the array
        if (indexOfStudentToDrop != -1) {
            for (i = indexOfStudentToDrop; i < numberOfStudents; i++)
                students[i] = students[i+1];
        } // end if found

        // decrement number of students by 1
        numberOfStudents--;
    } // end if equal


}

public void clear() {

    // iterating on the students array, assign all to null
    for (int i = 0; i < numberOfStudents; i++) {
        students[i] = null;
    }

    // assign number of students to zero
    numberOfStudents = 0;

}

public void increaseArray(int amount) {

    students = new String[students.length + amount];

}
}

public class TestCourse {
    public static void main(String[] args) {

        // create two courses
        Course course1 = new Course("Data Structures");
        Course course2 = new Course("Database Systems");

        // introduce the program
        System.out.println("Creating Two Courses");
        System.out.println("Adding 6 students to course 1");
        System.out.println("Adding 3 students to course 2");

        // add six students to course1
        course1.addStudent("\n1: Tom Servo");
        course1.addStudent("\n2: Joel Robinson");
        course1.addStudent("\n3: Mike Nelson");
        course1.addStudent("\n4: Pearl Forrester");
        course1.addStudent("\n5: TV's Frank");
        course1.addStudent("\n6: Zap Rowsdower");



        // add three students to course2
        course2.addStudent("\n1: Tom Servo");
        course2.addStudent("\n2: Crow T. Robot");
        course2.addStudent("\n3: Zap Rowsdower");

        // print out the number of students in each course
        System.out.println("Number of students in course1: " + course1.getNumberOfStudents() + " Students are: ");
        String[] students = course1.getStudents();
        for (int i = 0; i < course1.getNumberOfStudents(); i++)
            System.out.print(students[i] + " ");

        System.out.println();
        System.out.print("\nNumber of students in course2: " + course2.getNumberOfStudents() + " Students are: ");
        String[] students2 = course2.getStudents();
        for (int i = 0; i < course2.getNumberOfStudents(); i++)
            System.out.print(students2[i] + " ");

        // tell user how many students you plan to drop from each course
        System.out.println ("\n\ndropping 2 students from course 1");
        System.out.println ("\ndropping 1 student from course 2");

        // drop some students.
                course1.dropStudent("Tom Servo");
                course1.dropStudent("Joel Robinson");
                System.out.println ("\nNumber of students in Course 1: " + " Students are: " + "\n" + course1);

                course2.dropStudent("Crow T. Robot");
                System.out.println("\nNumber of students in Course 2: " + " Students are: " + "\n" + course2);

        // clear course2, but keep course1 as it currently stands
                System.out.println("\n\nclearing course 2 course 2");
                course2.clear();

                System.out.println("\nNumber of students in Course 1: " + " Students are: " + "\n" + course1);
                System.out.println("\nNumber of students in Course 2: " + " Students are: " + "\n" + course2);
        }

}

这应该是它的样子:

Creating Two Courses
Adding 6 students to course 1
Adding 3 students to course 2

Number of students in Course 1: 6 Students are: 
1: Tom Servo
2: Joel Robinson
3: Mike Nelson
4: Pearl Forrester
5: TV's Frank
6: Zap Rowsdower

Number of students in Course 2: 3 Students are: 
1: Tom Servo
2: Crow T. Robot
3: Zap Rowsdower

dropping 2 students from course 1
dropping 1 student from course 2

Number of students in Course 1: 4 Students are:
1: Mike Nelson
2: Pearl Forrester
3: TV's Frank
4: Zap Rowsdower

Number of students in Course 2: 2 Students are:
1: Tom Servo
2: Zap Rowsdower

clearing course 2 course 2

Number of students in Course 1: 4 Students are:
1: Mike Nelson
2: Pearl Forrester
3: TV's Frank
4: Zap Rowsdower

Number of students in Course 2: 0 Students are:

但是当我运行代码时会发生这种情况:

Creating Two Courses
Adding 6 students to course 1
Adding 3 students to course 2
Number of students in course1: 6 Students are: 

1: Tom Servo 
2: Joel Robinson 
3: Mike Nelson 
4: Pearl Forrester 
5: TV's Frank 
6: Zap Rowsdower 

Number of students in course2: 3 Students are: 
1: Tom Servo 
2: Crow T. Robot 
3: Zap Rowsdower 

dropping 2 students from course 1

dropping 1 student from course 2

Number of students in Course 1:  Students are: 
Course@6d06d69c

Number of students in Course 2:  Students are: 
Course@7852e922


clearing course 2 course 2

Number of students in Course 1:  Students are: 
Course@6d06d69c

Number of students in Course 2:  Students are: 
Course@7852e922

正如您所看到的,在初始列表之后,发生了两件事:

1)程序已停止计数 2)名称已停止列出

1 个答案:

答案 0 :(得分:0)

首先,您应该使用列表,因为您在删除和添加学生时更改了数组的大小。反正...

所以你在输出的底部看到的是Course对象的指针。换句话说,您正在查看存储在对象中的信息的方向,而不是信息本身。当您尝试打印没有toString()方法的对象时(例如,当您编写System.out.println(... + course1)时)会发生这种情况。此方法可让您基本设置打印对象时显示的内容以及覆盖的排序对象指针的默认值。

在这种情况下,您要显示的信息是学生&#39;姓名,有多少学生,可能还有课程名称。您的toString()方法(在课程类中)将看起来像这样:

public String toString(){

    String toReturn = "";

    toReturn += "Number of students in " + courseName + ": "
                    + numberOfStudents + " Students are:\n";

    for(int i = 0; i < number of students - 1; i++){

        toReturn += (i + 1) + ": " + students[i] + "\n";

    }

    return toReturn;

}

如果您将此方法添加到课程中,那么您甚至不需要所有代码打印在打印指针之前有效的信息。您可以按原样打印Course对象。