英文:
I M getting wrong output pls help>>>>>
问题
当我输入hourOfDay为-1、-2、-3......时,返回值应该为false,但实际上却为true :(
package com.company;
public class BarkingDog {
public static boolean shouldWakeUp(boolean barking, int hourOfDay){
if(barking && hourOfDay > 24 && hourOfDay < 0){
return false;
}
else if(barking && hourOfDay < 8 && hourOfDay < 23){
return true;
}
return false;
}
}
英文:
When I put the value of hourOfDay -1, -2, -3..... the returned value should be false but it is coming true
package com.company;
public class BarkingDog {
public static boolean shouldWakeUp(boolean barking, int hourOfDay){
if(barking && hourOfDay >24 && hourOfDay <0){
return false;
}
else if(barking && hourOfDay < 8 && hourOfDay < 23){
return true;
}
return false;
}
}
答案1
得分: 1
看起来你想在那里使用 ||
,而不是 &&
:
if (barking || (hourOfDay > 24 || hourOfDay < 0)) {
return false;
}
虽然整个方法似乎可以简化为一个表达式:
return barking && hourOfDay > 8 && hourOfDay < 23;
英文:
Looks like you meant to use ||
there instead of &&
:
if (barking && (hourOfDay >24 || hourOfDay <0)) {
return false;
}
Although it seems the entire method can be simplified to a single expression:
return barking && hourOfDay > 8 && hourOfDay < 23;
答案2
得分: 0
这对你有效。使用 && 同时适用于两个条件,你应该使用 ||
if (barking && (hourOfDay > 30 || hourOfDay < 0)) {
return false;
} else if (barking && hourOfDay < 8 && hourOfDay < 25) {
return true;
}
return false;
}
英文:
This will work for you. Check && is for both conditions and you should use OR ||
if (barking && (hourOfDay > 30 || hourOfDay < 0)) {
return false;
} else if (barking && hourOfDay < 8 && hourOfDay < 25) {
return true;
}
return false;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论