小编Sca*_*ner的帖子

boolean = true和只是boolean有什么区别

我正在做一个练习题,它是:

我们有一只吵闹的鹦鹉."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 = …

java boolean operators

2
推荐指数
1
解决办法
115
查看次数

标签 统计

boolean ×1

java ×1

operators ×1