繁体   English   中英

出现“运算符‘&&’不能应用于‘布尔值’、‘int’”错误,我不确定为什么

[英]Getting an "Operator '&&' cannot be applied to 'boolean', 'int'" error and I'm unsure why

我正在编写一种方法来确定是否有两个“课程”对象,其中“课程”object 由课程名称(字符串)、部门(字符串)、代码(整数)、节(字节)和讲师(字符串)组成), 如果对象具有等效值则返回“true”。 但是,在检查原始“课程”object 和新“课程”object 是否相等的方法部分,我收到上述错误。

参考代码:

public boolean equals(Course obj){
    if(obj instanceof Course){

        Course c = (Course)obj;

        if(this.courseName.equals(course.getName()) &&
                this.department.equals(course.getDepartment()) &&
                (this.code==course.getCode()) &&
                (Byte.compare(this.section, course.getSection())) &&
                this.instructor.equals(course.getInstructor()))
            return true;
    }
    return false;
}

错误被列为在线if(this.courseName.equals(course.getName()) && ,但我不确定它是否指的是整个 if 语句。

谢谢!

错误是指整个if语句。 Byte.compare()返回一个int ,它不能与逻辑运算符一起使用。

对于原始byte值,您可以只使用==

if(this.courseName.equals(course.getName()) &&
        this.department.equals(course.getDepartment()) &&
        this.code == course.getCode() &&
        this.section == course.getSection() &&
        this.instructor.equals(course.getInstructor())) {
   
    return true;
}

另请注意,您在字符串比较中存在NullPointerException风险。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM