运算符||不能包含在int中吗?

时间:2019-03-25 09:36:56

标签: java

public class shouldWakeUp {

    public static boolean shouldWakeUp(boolean barking, int hourOfTheDay) {

        if (barking) {
            if (hourOfTheDay=8 || hourOfTheDay=7 || hourOfTheDay=6 || hourOfTheDay=5 || hourOfTheDay=4 || hourOfTheDay=3 || hourOfTheDay=2 || hourOfTheDay=1 || hourOfTheDay=23 || hourOfTheDay=00) {
                return true;
            } else {
                return false;
            }
            if (hourOfTheDay < 0 || hourOfTheDay > 23)
                return false;
        }
    }
}

我无法理解该错误,它说:operator || cannot be included in int

2 个答案:

答案 0 :(得分:4)

使用此代码

public static boolean shouldWakeUp(boolean barking, int hourOfTheDay) {
    if (barking) {
        if (hourOfTheDay==8 || hourOfTheDay==7 || hourOfTheDay==6 || hourOfTheDay==5 || hourOfTheDay==4 || hourOfTheDay==3 || hourOfTheDay==2 || hourOfTheDay==1 || hourOfTheDay==23 || hourOfTheDay==00) {
            return true;
        } else {
            return false;
        }
        if (hourOfTheDay < 0 || hourOfTheDay > 23)
            return false;
    }
}

为了进行比较,您必须使用==而不是=

答案 1 :(得分:0)

语句中的条件应为布尔结果。 Java使用布尔结果(返回值)来操纵条件。

在您的代码中,您使用的是“ =”赋值运算符,它没有给出预期的返回值。

使用“ ==”解决了Ajmal在其中一个答案中建议的代码中的问题。

但是,在编辑之后,您的代码将在修复后包含无法访问的语句,在这种情况下,请尝试以下操作:

if (barking
            && (hourOfTheDay == 8 || hourOfTheDay == 7 || hourOfTheDay == 6
                    || hourOfTheDay == 5 || hourOfTheDay == 4
                    || hourOfTheDay == 3 || hourOfTheDay == 2
                    || hourOfTheDay == 1 || hourOfTheDay == 23 || hourOfTheDay == 00)) {
        return true;
    } else if (hourOfTheDay < 0 || hourOfTheDay > 23)
        return false;
    else {
        return false;
    }