2012-09-07 41 views
-1

我没有得到任何错误,但我不能加场添加对象在Java中

//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 

那么这意味着我不能addcourse为什么?我是一名新学员,所以我会很感激任何帮助。

+0

放入代码,您正在遍历'myCourseCatalog'并打印'Course' – SiB

+0

这里是课程中的打印信息方法; public String printInfo(){ \t \t String sentence =“”; \t \t sentence + =“Name:”+ name +“\ n”; \t \t句子+ =“讲师:”+讲师+“\ n”; \t \t sentence + =“Year:”+ year +“\ n”; \t \t句子+ =“章节数:”+ numberOfSection +“\ n”; \t \t return return; \t} –

+0

并确保在调用addCourse方法之前包含noOfCourse定义的代码以及对其值的任何修改。 –

回答

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 ++; 
} 
+0

OH MY GOD非常感谢。我爱你,兄弟!!!! –

+0

我也是;人!一切顺利...接受帮助你的答案:) – SiB

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

+0

哦谢谢你是的,它的工作原理 –

+0

@YigitCan为什么不使用数组或List? – oldrinb

+0

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/List.html o是你在说什么?我实际上不知道这样的类,这就是为什么我在我的程序中编写每一行的原因。 –