-1

我没有收到任何错误,但我无法添加课程

    //in my Course class i use equals method to check whether they are the sam
    public boolean equals (Course other){
    Course c = (Course) other;
    if(c != null){
        if (this.name.equals(c.name) && this.instructor.equals(c.instructor) && this.numberOfSection == (c.numberOfSection) && this.year == (c.year))
            return true;
        else
            return false;   
        }
    else 
        return false;
}

//in my CourseCatalog class i use the equals method in Course and if they are not same 
// i add the course to the catalog
public void addCourse (Course other) {
    if(other != null){
        if( !other.equals(course1) && !other.equals(course2) && !other.equals(course3) && !other.equals(course4))
        {
            if (noOfCourse == 0){
                course1 = new Course(other);
                noOfCourse ++;
            }
            if (noOfCourse == 1){
                course2 = new Course(other);
                noOfCourse ++;
            }
            if (noOfCourse == 2){
                course3 = new Course(other);
                noOfCourse ++;
            }
            if(noOfCourse == 3){
                course4 = new Course(other);
                noOfCourse ++;
            }   
        }
    }
}

            //the following code is what i do in the tester class 
            CourseCatalog myCourseCatalog  = new CourseCatalog();
    Course course1 = new Course();
    course1.setName("Math101");
    course1.setInstructor("Jack Smith");
    course1.setYear(2007);
    course1.setNumberOfSection(3);
    myCourseCatalog.addCourse(course1);

            // i add a different course 
    Course course2 = new Course("Cs101", "David Brown", 2003 ,3);
    myCourseCatalog.addCourse(course2);
    Course copyCourse = new Course(course2);
    myCourseCatalog.addCourse(copyCourse);

但是程序以这种方式打印出来;

Name: Math101
Instructor: Jack Smith
Year: 2007
Number Of Sections: 3Name: Math101
Instructor: Jack Smith
Year: 2007
Number Of Sections: 3Name: Math101
Instructor: Jack Smith
Year: 2007
Number Of Sections: 3Name: Math101
Instructor: Jack Smith
Year: 2007
Number Of Sections: 3

所以这意味着我不能添加课程为什么?我是java的新学生,所以我很感激任何帮助。

4

2 回答 2

2

哇这个美女。。。

您正在使用 if-else 链,以确保所有内容都if为真并被执行。首先if你检查noOfCourse == 0然后增加它;在下一个中,您检查noOfCourse==1哪个由于增量而为真。

所以当你第一次调用你的方法addCourse时,所有的课程都已经设置好了。

请更换

if (noOfCourse == 0){
    course1 = new Course(other);
    noOfCourse ++;
}
if (noOfCourse == 1){
    course2 = new Course(other);
    noOfCourse ++;
}
if (noOfCourse == 2){
    course3 = new Course(other);
    noOfCourse ++;
}
if(noOfCourse == 3){
    course4 = new Course(other);
    noOfCourse ++;
}   

if (noOfCourse == 0){
    course1 = new Course(other);
    noOfCourse ++;
} else if (noOfCourse == 1){
    course2 = new Course(other);
    noOfCourse ++;
} else if (noOfCourse == 2){
    course3 = new Course(other);
    noOfCourse ++;
} else if(noOfCourse == 3){
    course4 = new Course(other);
    noOfCourse ++;
}   
于 2012-09-07T04:43:42.237 回答
1

问题是您实际上遇到了级联if问题。见下文...

if (noOfCourse == 0){
    course1 = new Course(other);
    noOfCourse ++;
}
if (noOfCourse == 1){
    course2 = new Course(other);
    noOfCourse ++;
}
if (noOfCourse == 2){
    course3 = new Course(other);
    noOfCourse ++;
}
if(noOfCourse == 3){
    course4 = new Course(other);
    noOfCourse ++;
}   

不管是什么noOfCourse,它都会增加它,从而满足它if下面的内容。这会导致您的课程填满多个课程空缺。解决方案是使用else if.

if (noOfCourse == 0){
    course1 = new Course(other);
    noOfCourse ++;
} else if (noOfCourse == 1) {
    course2 = new Course(other);
    noOfCourse ++;
} else if (noOfCourse == 2) {
    course3 = new Course(other);
    noOfCourse ++;
} else if (noOfCourse == 3) {
    course4 = new Course(other);
    noOfCourse ++;
}   

这样,它只会添加一次课程。:-)

顺便说一句,您应该真正使用数组或List此处。

final Count[] courses = new Course[4];
final int coursesAssigned = 0;
...
if (coursesAssigned < 4) {
  courses[coursesAssigned++] = new Course(other);
}

另外,你为什么要复制other

于 2012-09-07T04:45:06.900 回答