java - boolean = true 和 just boolean 有什么区别

标签 java boolean operators

我正在做一个练习题:

我们有一只会说话的鹦鹉。 “小时”参数是当前小时时间,范围为 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/

相关文章:

java - 方法不适用于参数

c++ - boolean 运算在 C++ 类内部重载以及类内日期的 if 语句

c++ ->?= 运算符是什么意思?

java - 选择 jsoup 中属性为空值的元素

java - 简单的客户端服务器应用程序,但出了问题

java - 在 Spring Boot 中连接到任何一个数据库

algorithm - 评估表达式是否重言式

java - 以下代码片段不会产生预期结果

mysql - 了解mysql子查询编写长尾查询

java - JVM 在创建线程并在其中执行网络操作时突然关闭