Sca*_*ner 2 java boolean operators
我正在做一个练习题,它是:
我们有一只吵闹的鹦鹉."hour"参数是0..23范围内的当前小时时间.如果鹦鹉在说话,小时在7点之前或者在20点之后,我们就会遇到麻烦.如果我们遇到麻烦,请回复.
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;
}`
Run Code Online (Sandbox Code Playgroud)
正确答案是:
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 +
}
Run Code Online (Sandbox Code Playgroud)
我想知道talk = true和just talk之间有什么区别.
| 归档时间: |
|
| 查看次数: |
115 次 |
| 最近记录: |