Java 中的 if(布尔条件)

if (boolean condition) in Java

提问人:rickygrimes 提问时间:10/3/2013 最后编辑:0xCursorrickygrimes 更新时间:11/21/2022 访问量:254229

问:

这总是让我感到非常困惑。有人可以解释一下吗?我的困惑是 - 布尔默认值为 false。所以在下面的例子中,它是在状态未打开时进入循环,即关闭还是当状态打开时进入循环,换句话说?ifif (condition is false)ifif (condition is true)

boolean turnedOn;
if (turnedOn) {
    //do stuff when the condition is false or true?
} else {
    //do else of if
}

我知道这是一个非常基本的问题,但如果你能用非常基本的语言解释答案,那就太好了。:)请随时向我指出具有很好解释的重复帖子(我没有找到一个可以清楚地理解它的帖子)。如果您想使其更通用,也可以随意更改帖子的主题。

java if 语句

评论

5赞 Juned Ahsan 10/3/2013
这是否编译: boolean state = “TURNED ON”; ??

答:

1赞 logoff 10/3/2013 #1
boolean state = "TURNED ON";

不是 Java 有效代码。boolean 只能接收布尔值(true 或 false),并且是一个 String。"TURNED ON"

编辑

现在你说的是一个循环,你的代码不包含任何循环。你的 var 是 false,因为布尔默认值,你执行了 else 子句。state

评论

0赞 rickygrimes 10/3/2013
对不起。在我的应用程序中,参数 TURNED ON 通过属性文件设置为 false。我从代码中复制粘贴了它。我刚刚编辑了它。感谢您让我知道,以免使该帖子的其他读者感到困惑。
0赞 sp00m 10/3/2013 #2

布尔值默认值仅对类的字段为 false。如果在方法中,则必须通过 true 或 false 初始化变量。因此,例如,在您的情况下,您将遇到编译错误。

此外,我真的不明白这一点,但是在if中输入的唯一方法是将条件评估为true。

评论

0赞 fluminis 10/3/2013
小写字母 B 的布尔字段的默认值为 false。带有大写字母 B 的布尔字段的默认值为 null。
0赞 Juned Ahsan 10/3/2013 #3

假设在实际代码中设置了有效的布尔值,则以下条件将成功state

if(state)

当 state 为 boolean 值为 TRUE 时

如果条件检查表达式是否计算为 TRUE/FALSE。如果表达式很简单,则条件将成功。true

评论

0赞 rickygrimes 10/3/2013
有效布尔值是什么意思?如果状态为 true,或者当它为 false 时,它会进入 if 循环吗?
0赞 Juned Ahsan 10/3/2013
@rickygrimes我已经对这个问题发表了评论。布尔值的有效值为 true 或 false。您在示例中提到了“打开”
0赞 Rahul 10/3/2013 #4

这就是行为方式。if

if(turnedOn) // This turnedOn should be a boolean or you could have a condition here which would give a boolean result.
{
// It will come here if turnedOn is true (i.e) the condition in the "if" evaluates to true
}
else
{
// It will come here if turnedOn is false (i.e) the condition in the "if" evaluates to false
}
33赞 Jeff 10/3/2013 #5

好吧,所以..

// As you already stated, you know that a boolean defaults to false.
boolean turnedOn;
if(turnedOn) // Here, you are saying "if turnedOn (is true, that's implicit)
{
//then do this
}
else // if it is NOT true (it is false)
{
//do this
}

现在更有意义了吗?

该语句将计算您在其中输入的返回布尔值的任何代码,如果计算返回 true,则输入第一个块。否则(如果值为 true,它将是假的,因为布尔值可以是 true 或 false)它将进入 - 是的,你猜对了 - 块。ifelse {}

一个更详细的例子。

如果有人问我“你饿了吗?”,简单的回答是肯定的(真的)。或否(false)。

boolean isHungry = true; // I am always hungry dammit.
if(isHungry) { // Yes, I am hungry.
   // Well, you should go grab a bite to eat then!
} else { // No, not really.
   // Ah, good for you. More food for me!

   // As if this would ever happen - bad example, sorry. ;)
}

评论

0赞 rickygrimes 10/3/2013
如果 turnedOn 实际上是假的怎么办。假设在我上面的代码中,我有类似的东西 - boolean turnedOn=false,然后代码的其余部分是相同的,然后呢?
0赞 rickygrimes 10/3/2013
有人可以回答这个问题吗?
1赞 Jeff 10/3/2013
然后它将进入块,如上所述。在语句中,你隐含地询问 turnedOn 是否为 true - 你也可以这样做,然后你将输入第一个块(因为当 turnedOn 为 false 时,则 turnedOn == false 为 true)。elseifif(turnedOn == false)
1赞 Jeff 10/3/2013
@rickygrimes 太好了,很高兴我能帮上忙。去吃点零食,brb。
0赞 Simon Dorociak 10/3/2013 #6
if (turnedOn) {
    //do stuff when the condition is false or true?
}
else {
    //do else of if
}

它可以写成:

if (turnedOn == true) {
        //do stuff when the condition is false or true?
    }
else { // turnedOn == false or !turnedOn
        //do else of if
}

因此,如果你的变量是 true,if 将被调用,if 被赋值给 false,else 将被调用。布尔值将隐式分配给 false,如果不显式分配它们,则 e.q.turnedOnturnedOn = true

1赞 TheLostMind 10/3/2013 #7
boolean turnedOn;
    if(turnedOn)
    {
    //do stuff when the condition is true - i.e, turnedOn is true
    }
    else
    {
    //do stuff when the condition is false - i.e, turnedOn is false
    }
14赞 Safinn 10/3/2013 #8

在您的示例中,IF 语句将在 state = true 时运行,这意味着 else 部分将在 state = false 时运行。

if(turnedOn == true) is the same as if(turnedOn)

if(turnedOn == false) is the same as if(!turnedOn)

如果您有:

boolean turnedOn = false;

boolean turnedOn;

然后

if(turnedOn)
{

}
else
{
    // This would run!
}
0赞 Subir Kumar Sao 10/3/2013 #9

if block 的语法如下,

if(condition){
// Executes when condition evaluates to true.
}
else{
// Executes when condition evaluates to false.
}

在您的情况下,您直接传递布尔值,因此不需要计算。

4赞 Albert Laure 10/3/2013 #10

ABoolean(大写字母“B”)是一个布尔对象,如果未赋值,则默认为 null。布尔值(带有小写字母“b”)是一个布尔原语,如果未分配值,则默认为 false。

Boolean objectBoolean;
boolean primitiveBoolean;

System.out.println(objectBoolean); // will print 'null'
System.out.println(primitiveBoolean); // will print 'false'

引文

因此,在您的代码中,由于声明了带有小“b”的布尔值,因此它将设置为 false

boolean turnedOn;
    if(turnedOn) **meaning true**
    {
    //do stuff when the condition is false or true?
    }
    else
    {
    //do else of if ** itwill do this part bechae it is false
    }

if(turnedon) 测试一个值 if true,你没有为 turned on 赋值,使其为 false,使其执行 else 语句:)

评论

0赞 Albert Laure 10/3/2013
@Jeff是的,但没有人注意到它。
0赞 Jeff 10/3/2013
这真的太糟糕了。
1赞 Arafe Zawad Sajid 11/6/2016 #11

假设您要检查布尔值。如果为 true,请执行某些操作。否则,做点别的事情。你可以写:

if(condition==true){

}
else{   //else means this checks for the opposite of what you checked at if

}

取而代之的是,您可以简单地做到这一点:

if(condition){  //this will check if condition is true 

}
else{ 

}

负。如果条件为假,则要执行某些操作,如果条件为真,则执行其他操作。然后你会写:

if(condition!=true){   //if(condition=false)

}
else{

}

但遵循简单的路径。我们做:

if(!condition){  //it reads out as: if condition is not true. Which means if condition is false right?

}
else{

}

想想吧。你很快就会得到它。

0赞 LBIRD S.E. 11/21/2022 #12

每次条件“if (turnedOn)”总是称为“TRUE 条件”,除非条件是“if (!turnedOn)”将称为“FALSE 条件”。

在其他情况下,例如,如果您想比较两个布尔条件;

两个布尔变量:turnedOn、switchedOn

假设当前状况;

turnedOn=真 switchedOn=假

  • "if (turnedOn) || if (switchedOn)“ 将返回 TRUE
  • "if (turnedOn) & if (switchedOn)“ 将返回 FALSE