英文:
I cant seem to get the example output, my code returns true, true, false and false. Is my ranging not missing any additional conditional statement?
问题
这是您提供的代码:
public static boolean shouldWakeUp(boolean barking, int hourOfDay) {
if (hourOfDay < 0 || hourOfDay >= 23) {
return false;
}
if (hourOfDay < 8 || hourOfDay >= 22) {
return true;
} else {
return false;
}
}
如果需要修改的话,可以考虑将条件简化为以下方式:
public static boolean shouldWakeUp(boolean barking, int hourOfDay) {
if (hourOfDay < 0 || hourOfDay >= 23) {
return false;
}
return barking && (hourOfDay < 8 || hourOfDay >= 22);
}
英文:
I've the following scenario:
We have a dog that likes to bark. We need to wake up if the dog is barking at night!
Write a method shouldWakeUp that has 2 parameters.
1st parameter should be of type boolean and be named barking it represents if our dog is currently barking.
2nd parameter represents the hour of the day and is of 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 in that case return true.
In all other cases return false.
If the hourOfDay parameter is less than 0 or greater than 23 return false.
Examples of input/output:
shouldWakeUp (true, 1)
→ should returntrue
shouldWakeUp (false, 2)
→ should returnfalse
since the dog is not barking.shouldWakeUp (true, 8)
→ should returnfalse
, since it's not before 8.shouldWakeUp (true, -1)
→ should returnfalse
since thehourOfDay
parameter needs to be in a range 0-23.
and this is my code:
public static boolean shouldWakeUp(boolean barking, int hourOfDay) {
if (hourOfDay < 0 || hourOfDay >= 23) {
return false;
}
if (hourOfDay < 8 || hourOfDay >= 22) {
return true;
} else {
return false;
}
}
答案1
得分: 2
public static boolean shouldWakeUp(boolean barking, int hourOfDay) {
if (!barking)
return false;
if (hourOfDay < 0 || hourOfDay > 23)
return false;
return hourOfDay < 8 || hourOfDay >= 22;
}
**输出:**
System.out.println(shouldWakeUp(true, 1)); // true
System.out.println(shouldWakeUp(false, 2)); // false
System.out.println(shouldWakeUp(true, 8)); // false
System.out.println(shouldWakeUp(true, -11)); // false
英文:
You forgot to use boolean barking
and hourOfDay
could be 23
:
public static boolean shouldWakeUp(boolean barking, int hourOfDay) {
if (!barking)
return false;
if (hourOfDay < 0 || hourOfDay > 23)
return false;
return hourOfDay < 8 || hourOfDay >= 22;
}
Output:
System.out.println(shouldWakeUp(true, 1)); // true
System.out.println(shouldWakeUp(false, 2)); // false
System.out.println(shouldWakeUp(true, 8)); // false
System.out.println(shouldWakeUp(true, -11)); // false
答案2
得分: 0
public static boolean shouldWakeUp(boolean barking, int hourOfDay) {
if (hourOfDay < 0 || hourOfDay > 23) {
return false;
} else if (barking && (hourOfDay < 8 || hourOfDay > 22)) {
return true;
} else {
return false;
}
}
英文:
public static boolean shouldWakeUp(boolean barking, int hourOfDay) {
if (hourOfDay < 0 || hourOfDay > 23) {
return false;
} else if (barking && (hourOfDay < 8 || hourOfDay > 22)) {
return true;
} else {
return false;
}
}
答案3
得分: 0
public class Dog {
public static void main(String[] args) {
boolean a = shouldWakeUp(false, 2); // 适用于所有值的正常工作
System.out.println(a);
}
public static boolean shouldWakeUp(boolean barking, int hourOfDay) {
if ((barking) && ((hourOfDay > 22) || (hourOfDay < 8)) && (hourOfDay >= 0) && (hourOfDay <= 23)) {
return true;
}
return false;
}
}
英文:
public class Dog {
public static void main(String[] args) {
boolean a=shouldWakeUp(false,2);//works fine for all values
System.out.println(a);
}
public static boolean shouldWakeUp(boolean barking,int hourOfDay) {
if((barking)&&((hourOfDay>22)||(hourOfDay<8))&&(hourOfDay>=0)&&(hourOfDay<=23)) {
return true;
}return false;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论