简体   繁体   English

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

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

I'm writing a method that is to determine if two "Course" objects, where a "Course" object is comprised of courseName (String), department (String), code (int), section (byte), and instructor (String), and returns "true" if the objects have equivalent values.我正在编写一种方法来确定是否有两个“课程”对象,其中“课程”object 由课程名称(字符串)、部门(字符串)、代码(整数)、节(字节)和讲师(字符串)组成), 如果对象具有等效值则返回“true”。 However, in the portion of the method that checks if they original "Course" object and the new "Course" object are equal, I am getting the above error.但是,在检查原始“课程”object 和新“课程”object 是否相等的方法部分,我收到上述错误。

Code for reference:参考代码:

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

Error is listed as being on the line if(this.courseName.equals(course.getName()) && , but I'm unsure if it's referring to the entire if statement.错误被列为在线if(this.courseName.equals(course.getName()) && ,但我不确定它是否指的是整个 if 语句。

Thank you!谢谢!

The error is referring to the entire if statement.错误是指整个if语句。 Byte.compare() returns an int , which cannot be used with logical operators. Byte.compare()返回一个int ,它不能与逻辑运算符一起使用。

For primitive byte values, you can just use == :对于原始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;
}

Also note that you have a NullPointerException risk in your string comparisons.另请注意,您在字符串比较中存在NullPointerException风险。

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

相关问题 错误信息:运算符 &lt; 不能应用于 boolean,int - Error message: operator < cannot be applied to boolean,int 错误消息“运算符'&&'无法应用于'int''boolean' - Error message "Operator '&&' cannot be applied to 'int' 'boolean' 我收到此错误消息并了解原因:不能应用于给定类型; 必需:未找到增强:int,int - I'm getting this error message and understand why: cannot be applied to given types; required: no augments found: int, int 运算符 &amp;&amp; 不能应用于 &#39;boolean&#39;, &#39;int - Operator && cannot be applied to 'boolean', 'int 运算符“&”不能应用于byte,int,boolean - Operator “&” cannot be applied to byte, int, boolean 我的代码出现错误:不兼容的类型:int无法转换为布尔线:6 - I'm getting an error for my code: incompatible types: int cannot be converted to boolean line:6 为什么我会收到此错误? “二元运算符“&amp;&amp;”的操作数类型错误 第一种类型:int 第二种类型:布尔值 - Why am I getting this error? "Bad operand type for binary operator "&&" first type: int second type: boolean" 操作员! 不能应用于int - Operator ! cannot be applied to int 运算符 ',=' 不能应用于 'int'、'int[]' - Operator '!=' cannot be applied to 'int', 'int[]' 你能告诉我为什么我收到“字符串无法转换为 int”错误 - Can you tell me why I'm getting a "string cannot be converted to int" error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM