在Java中添加对象
我没有收到任何错误,但我无法添加课程在Java中添加对象,java,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.instruc
//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
那就意味着我无法解释为什么?我是爪哇的一名新生,所以我非常感谢任何帮助。哇,这真是一个美人 您使用的是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
问题。见下文
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 (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 ++;
}
这样,它只会添加一次课程。:-)
顺便说一下,您应该在这里真正使用数组或列表
final Count[] courses = new Course[4];
final int coursesAssigned = 0;
...
if (coursesAssigned < 4) {
courses[coursesAssigned++] = new Course(other);
}
final Count[]课程=新课程[4];
最终int课程分配=0;
...
如果(课程分配<4){
课程[coursesAssigned++]=新课程(其他);
}
此外,您为什么要复制其他
?在迭代myCourseCatalog
并打印课程
的代码中输入以下是课程类中的打印信息方法;公共字符串printInfo(){String-station=”“;句子+=”名称:“+Name+”\n”;句子+=”讲师:“+讲师+”\n”;句子+=”年份:“+Year+”\n”;句子+=”节数:“+numberOfSection+”\n”;返回句子;}在调用addCourse方法之前,请确保包含定义noOfCourse的代码以及对其值的任何修改。{String a=course1.printInfo();String b=course2.printInfo();String c=course3.printInfo();String d=course4.printInfo();if(noOfCourse>0){System.out.print(a);}如果(noOfCourse>1){System.out.print(b);}如果(noOfCourse>2){System.out.print(c);}如果(noOfCourse>3){System.out.print(d);}你已经得到了答案,我想,只是一个建议,试着使用一个集合类,这样你就不用担心这些事情了;伙计!祝你一切顺利。。。接受帮助您的答案:)@YigitCan为什么不使用数组或列表?你是这么说的吗?我不知道这些类,这就是为什么我要在我的programs@YigitCan是的,您可以将所有课程保留在一个数组或列表中,而不是为每个时段设置单独的变量。PS您应该使用最新的文档,即Java 7(1.7)而不是1.4.2。
final Count[] courses = new Course[4];
final int coursesAssigned = 0;
...
if (coursesAssigned < 4) {
courses[coursesAssigned++] = new Course(other);
}