提问人:rickygrimes 提问时间:10/3/2013 最后编辑:0xCursorrickygrimes 更新时间:11/21/2022 访问量:254229
Java 中的 if(布尔条件)
if (boolean condition) in Java
问:
这总是让我感到非常困惑。有人可以解释一下吗?我的困惑是 - 布尔默认值为 false。所以在下面的例子中,它是在状态未打开时进入循环,即关闭还是当状态打开时进入循环,换句话说?if
if (condition is false)
if
if (condition is true)
boolean turnedOn;
if (turnedOn) {
//do stuff when the condition is false or true?
} else {
//do else of if
}
我知道这是一个非常基本的问题,但如果你能用非常基本的语言解释答案,那就太好了。:)请随时向我指出具有很好解释的重复帖子(我没有找到一个可以清楚地理解它的帖子)。如果您想使其更通用,也可以随意更改帖子的主题。
答:
boolean state = "TURNED ON";
不是 Java 有效代码。boolean 只能接收布尔值(true 或 false),并且是一个 String。"TURNED ON"
编辑:
现在你说的是一个循环,你的代码不包含任何循环。你的 var 是 false,因为布尔默认值,你执行了 else 子句。state
评论
布尔值默认值仅对类的字段为 false。如果在方法中,则必须通过 true 或 false 初始化变量。因此,例如,在您的情况下,您将遇到编译错误。
此外,我真的不明白这一点,但是在if中输入的唯一方法是将条件评估为true。
评论
假设在实际代码中设置了有效的布尔值,则以下条件将成功state
if(state)
当 state 为 boolean 值为 TRUE 时
如果条件检查表达式是否计算为 TRUE/FALSE。如果表达式很简单,则条件将成功。true
评论
这就是行为方式。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
}
好吧,所以..
// 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)它将进入 - 是的,你猜对了 - 块。if
else {}
一个更详细的例子。
如果有人问我“你饿了吗?”,简单的回答是肯定的(真的)。或否(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. ;)
}
评论
else
if
if(turnedOn == false)
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.turnedOn
turnedOn = true
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
}
在您的示例中,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!
}
if block 的语法如下,
if(condition){
// Executes when condition evaluates to true.
}
else{
// Executes when condition evaluates to false.
}
在您的情况下,您直接传递布尔值,因此不需要计算。
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 语句:)
评论
假设您要检查布尔值。如果为 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{
}
想想吧。你很快就会得到它。
每次条件“if (turnedOn)”总是称为“TRUE 条件”,除非条件是“if (!turnedOn)”将称为“FALSE 条件”。
在其他情况下,例如,如果您想比较两个布尔条件;
两个布尔变量:turnedOn、switchedOn
假设当前状况;
turnedOn=真 switchedOn=假
- "if (turnedOn) || if (switchedOn)“ 将返回 TRUE
- "if (turnedOn) & if (switchedOn)“ 将返回 FALSE
评论