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

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


长风秋雁
浏览 95回答 2
2回答

不负相思意

使用此代码public static boolean shouldWakeUp(boolean barking, int hourOfTheDay) {&nbsp; &nbsp; if (barking) {&nbsp; &nbsp; &nbsp; &nbsp; if (hourOfTheDay==8 || hourOfTheDay==7 || hourOfTheDay==6 || hourOfTheDay==5 || hourOfTheDay==4 || hourOfTheDay==3 || hourOfTheDay==2 || hourOfTheDay==1 || hourOfTheDay==23 || hourOfTheDay==00) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (hourOfTheDay < 0 || hourOfTheDay > 23)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }}为了进行比较,您必须使用而不是===

ibeautiful

语句中的条件应给出布尔结果。Java 使用布尔结果(返回值)来操作条件。在代码中,您使用的是“=”赋值运算符,它不给出预期的返回值。使用“==”可以解决代码中的问题,如Ajmal在其中一个答案中所建议的那样。但是,在编辑后,您的代码将在修复后具有无法访问的语句,在这种情况下,请尝试以下操作:if (barking&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && (hourOfTheDay == 8 || hourOfTheDay == 7 || hourOfTheDay == 6&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || hourOfTheDay == 5 || hourOfTheDay == 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || hourOfTheDay == 3 || hourOfTheDay == 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || hourOfTheDay == 1 || hourOfTheDay == 23 || hourOfTheDay == 00)) {&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; } else if (hourOfTheDay < 0 || hourOfTheDay > 23)&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java