布尔值在Java的if语句中是如何工作的?

huangapple go评论86阅读模式
英文:

How do booleans work in if statements in Java?

问题

以下是您提供的代码的翻译部分:

我正在尝试理解 `for` 循环的基本原理以及它们如何与 `boolean` 值一起工作下面是我编写的几行简单的代码但我不太理解输出结果为什么是这样的

    public class notes {
        public static void main(String[] args) {
            boolean[] test = {false, false, true};
            if (test[0] = false) {
                System.out.println("yes");
            } else {
                System.out.println("no");
            }
        }
    }

我会认为因为测试数组中索引 0 处的 boolean 值为 false所以这个 if 语句应该打印出 yes然而它却打印出了 no为什么会这样呢
英文:

I'm trying to understand the basics of for loops and how they work with booleans. There is a simple few lines of code that I wrote and I don't quite understand why the output is what it is.

public class notes { 
    public static void main(String[] args) {
        boolean[] test = {false, false, true};
        if (test[0] = false) {
           System.out.println("yes");
        } else {
    	   System.out.println("no");
        }
    }
}

I would assume that because the boolean value at index 0 in the test array is false, then this if statement should also print yes. However, it prints no. Why is this?

答案1

得分: 3

尝试这样做。由于条件语句的结果是true或false,你可以直接使用布尔值本身。由于你希望在条件为false时打印"yes",所以你需要将条件设置为true,以便条件语句成功。因此,通过在条件之前加上感叹号!(也称为非运算符)来取反条件。

public class notes {
    public static void main(String[] args) {
       boolean[] test = {false, false, true};
    
       // 如果为false就执行
       if(!test[0]) {
         System.out.println("yes");  
       } else {
          System.out.println("no");
       }
     }
}
英文:

Try it like this. Since conditionals result in a true or false condition you can just use the boolean by itself. Since you want it to print yes when false, you need to make it true so the conditional will succeed. So invert the condition by prefixing a bang ! (aka the NOT operator).

public class notes {
    public static void main(String[] args) {
       boolean[] test = {false, false, true};
    
       // if false do it.
       if(!test[0]) {
         System.out.println("yes");  
       } else {
          System.out.println("no");
       }
     }
}
   

</details>



# 答案2
**得分**: 1

以下是翻译好的部分:

你正在使用赋值运算符`=`,而不是使用比较运算符`==`,因此你*没有进行比较*,而是将`false`值赋给了数组的索引0,然后同样的`false`被解释为布尔值;因此,`else`块会执行。

请尝试使用以下代码:

```java
public class notes {

    public static void main(String[] args) {

        boolean[] test = {false, false, true};

        if(test[0] == false) { //你在这里犯了一个错误。
           System.out.println("yes");
        } else {
           System.out.println("no");
        }
    }
}

请注意,你还可以使用!一元运算符对布尔表达式取反,例如!booleanValue。在你的情况下,可以写成!test[0]

在这里查看Java中的运算符。

英文:

You are using an assignment operator =, instead of using a comparison operator ==, hence you don't compare, but rather assign a false value to the index 0 of your array and the same false is then evaluated as a boolean value; therefore, else block executes.

Try this instead:

public class notes { 

    public static void main(String[] args) {

        boolean[] test = {false, false, true};

        if(test[0] == false) { //you had a mistake here.
           System.out.println(&quot;yes&quot;);
        } else {
           System.out.println(&quot;no&quot;);
        }
    }
}

Note, that you can alternatively negate boolean expression with ! unary operator, like !booleanValue. In your case it would look like !test[0].

Have a look at operators in Java.

答案3

得分: 1

你将 false 赋值给数组的第一个元素,这会导致评估结果为 false,从而执行 else 分支。你需要使用 == 进行比较,在处理布尔值时,可以直接使用逻辑非运算符 (!) 来检查是否为 false。比较布尔值总是多余的,因为比较的结果本身就是一个布尔值。

if (!test[0]) {
  System.out.println("yes");
} else {
  System.out.println("no");
}
英文:

You are assigning false to the first element of the array, which evaluates to false and causes the else branch to be executed instead. You need to use == for comparison and in the case of booleans, you can simply use the logical not operator (!) to check if it is false. It is always redundant to compare boolean values, as the result of comparison is a boolean.

if(!test[0]) {
  System.out.println(&quot;yes&quot;); 
} else {
  System.out.println(&quot;no&quot;);
}

huangapple
  • 本文由 发表于 2020年8月20日 00:29:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63491261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定