我正在做一个练习题:
我们有一只会说话的鹦鹉。 “小时”参数是当前小时时间,范围为 0..23。如果鹦鹉在说话并且时间在 7 点之前或 20 点之后,我们就有麻烦了。如果我们有麻烦,则返回 true。
parrotTrouble(true, 6) → true parrotTrouble(true, 7) → false parrotTrouble(false, 6) → false
我的代码是:
`public boolean parrotTrouble(boolean talking, int hour) {
if ((talking = true) && (hour < 7 || hour > 20)){
return true;
}
else
return false;
}`
正确答案是:
public boolean parrotTrouble(boolean talking, int hour) {
return (talking && (hour < 7 || hour > 20));
// Need extra parenthesis around the || clause
// since && binds more tightly than ||
// && is like arithmetic *, || is like arithmetic +
}
我想知道talking = true 和just talk 之间有什么区别。
最佳答案
talking = true
将 true 分配给 talking
并返回 true。
if (talking == true)
与 if (talking)
相同,因为两者都返回 true。
关于java - boolean = true 和 just boolean 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31471637/