gpt4 book ai didi

java - 为什么第一个 if 语句失败?

转载 作者:太空宇宙 更新时间:2023-11-04 09:14:26 26 4
gpt4 key购买 nike

编辑:很抱歉让你们中的一些人感到困惑,这里有更多细节。

我对 stackoverflow 很陌生。所以我提前为一个愚蠢的问题道歉。

我在 Udemy 中遇到了以下练习,但我的代码失败了。请帮助我理解为什么失败?

练习描述:

Write a method shouldWakeUp that has 2 parameters.

1st parameter should of type boolean and be named barking it represents if our dog is currently barking

2nd parameter represents the hour of day and is of the type int with the name hourOfDay and has a valid range of 0-23

we have to wake up if the dog is barking before 8 or after 22 hours so that in that case return true, in all other case return false.

if the hourOfDay parameter is less than 0 or greater 23 return false

    public class BarkingDog {

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

if (hourOfDay < 8 || hourOfDay > 22) {
return true;
} else if (hourOfDay < 0 || hourOfDay > 23) {

}
return false;
}
}

最佳答案

您的第一个语句失败,因为您没有检查它的吠声和条件是否小于 0 或大于 23。

您需要执行以下操作

public static boolean shouldWakeUp(boolean barking, int hourOfDay) {
if (barking) {
if ((hourOfDay > 0 && hourOfDay < 8) || (hoursOfDay > 22 && hoursOfDay <23)) {
return true;
}
}
return false;
}

关于java - 为什么第一个 if 语句失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59240021/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com