代码赢得';Don’不要让学生放弃一门课程[Java]

代码赢得';Don’不要让学生放弃一门课程[Java],java,class,oop,object,Java,Class,Oop,Object,和我一样的问题,但我想我离解决它越来越近了 简言之,我的代码有两门课程,我必须从每门课程中删除一些学生,然后完全清除第二门课程。此代码使用两个类完成 这就是我到目前为止所做的: /* Program description: Starting with listing 10.5 and 10.6 below, you are to modify the two source files to: Course.java: add method increaseArray(). Increa

和我一样的问题,但我想我离解决它越来越近了

简言之,我的代码有两门课程,我必须从每门课程中删除一些学生,然后完全清除第二门课程。此代码使用两个类完成

这就是我到目前为止所做的:

/*
Program description:
Starting with listing 10.5 and 10.6 below, you are to modify the two source
files to:

Course.java:


 add method increaseArray().
Increase the array when students are added by 1, (hint utilize
System.arraycopy method ). Then add the student to the last location. I
would suggest you set the size of the “students” array to a size initially
of zero, so when the any student is added, your code to increase the
array will be triggered.

add method dropStudent().
Drop student would need to find the String match by iterating on the
students array using the .equals String method. When a match is found,
remove that entry by assigning the next student into the match location,
and do for all the remaining students (effectively shift remaining
students by one ). Also you would have also decrement the
numberOfStudents count.

 add method clear().
Iterating on the students array assign all to null and set the
numberOfStudents count to 0.
TestCourse.java:
You need to test your new methods; this is done by modifying listing 10.5 to
meet the requirements of assignment which are:

Create a two courses
Add six students to the first, three students to the second
Remove the first two student from course one
Remove the second student from course two
Display students in both classes
Clear the second course
Display students in both classes


Add a static method to TestCourse that handles printing the students in a
class. This method receives the reference of the course and the course
number. This way, you just call the method to print out the students.
 */

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) {

    int IndexOfStudentToDrop = -1;

    for (int i = 0; i < numberOfStudents; i++) {
        if (students[i].equalsIgnoreCase(student)) {
            IndexOfStudentToDrop = i;
            if (IndexOfStudentToDrop != -1) {
                for (i = IndexOfStudentToDrop; i < numberOfStudents; i++)
                    students[i] = students[i+1];
            } // end if found
            // decrement number of students by 1
            numberOfStudents--;
        }
    }

}

public void clear() {

    for (int i = 0; i < numberOfStudents; i ++){
        students [i] = null;
    }
    numberOfStudents = 0;

}

public void increaseArray() {
    if (numberOfStudents >= students.length) {
        String[] temp = new String[students.length * 2];
        System.arraycopy(students, 0, temp, 0, students.length);
        students = temp;
    }



} // end of increaseArray()

public String toString ()
{
    String output = "";
     output += getCourseName()+ (getNumberOfStudents() + "students");
        for (int i = 0; i < getNumberOfStudents(); i++) {
                output += "\n("+ (i + 1)+")"+ students [i];
            }
    return output;

}
}
但事实恰恰相反:

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 Rowsdowerdropping 2 students from course 1
dropping 1 student from course 2

Number of students in Course 1: 6 Students are: 

Number of students in Course 2: 3 Students are: 

clearing course 2 course 2

Number of students in Course 1: 6 Students are: 

Number of students in Course 2: 0 Students are: 

正如您所看到的,我的代码不会在需要时删除学生(尽管清除似乎不是问题)。我知道我的格式有问题,但这不是我最关心的问题,我可以自己解决。除了格式设置之外,我唯一的问题是学生的辍学,如果我能得到帮助的话,我可以从这里开始解决。

这不起作用的原因如下:你把字符串像
1:Tom Servo
和数字放在一起,然后试图像
course1.dropStudent(“Tom Servo”)

如果在添加
学生时删除
1:
,或者尝试将其删除为:

course1.dropStudent("1: Tom Servo");

我确信它会起作用。

你的
学生在哪里?对比较学生的方法特别好奇(
equalsIgnoreCase
)Student是用于从某个类中删除学生的字符串的名称。好的,它在执行“dropStudent”后计算学生人数,但在删除后不会列出学生。没关系,我只需要重新使用for循环来打印新的学生列表。这最终是问题所在吗?如果是这样,而且已经解决了,我恳请您接受答案,结束这个问题。:)
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 Rowsdowerdropping 2 students from course 1
dropping 1 student from course 2

Number of students in Course 1: 6 Students are: 

Number of students in Course 2: 3 Students are: 

clearing course 2 course 2

Number of students in Course 1: 6 Students are: 

Number of students in Course 2: 0 Students are: 
course1.dropStudent("1: Tom Servo");