[英]if (boolean condition) in Java
這對我來說總是很困惑。 有人可以解釋一下嗎? 我的困惑是 - 布爾默認為false。 因此在下面的示例中,當狀態未打開時它是否進入if
循環,即, if (condition is false)
它是TURNED OFF if (condition is false)
或者當狀態為TURNED ON時它是否進入if
循環,換句話說if (condition is true)
?
boolean turnedOn;
if (turnedOn) {
//do stuff when the condition is false or true?
} else {
//do else of if
}
我知道這是一個非常基本的問題,但如果你能用非常基本的語言解釋答案,那就太好了。 :)隨意指出我復制的帖子有一個非常好的解釋(我沒有找到一個我可以清楚得到它)。 如果您想使其更通用,也可以隨意更改帖子的主題。
可以,然后呢..
// 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
}
現在更有意義嗎?
if
語句將評估您放入其中的任何返回布爾值的代碼,如果評估返回true,則輸入第一個塊。 否則 (如果值不為真,它將為false,因為布爾值可以為true或false)它將進入 - 是的,你猜對了 - else {}
塊。
一個更冗長的例子。
如果我被問到“你餓了嗎?”,簡單的回答是肯定的(真實的)。 或者不(假)。
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. ;)
}
在您的示例中,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!
}
ABoolean(帶有大寫'B')是一個Boolean對象,如果沒有賦值,則默認為null。 boolean(帶小寫'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(turnon)測試一個值,如果為true,你沒有為啟用賦值使其成為假,使得它執行else語句:)
boolean state = "TURNED ON";
不是Java有效代碼。 boolean只能接收布爾值(true或false), "TURNED ON"
是一個String。
編輯 :
現在你在談論一個循環而你的代碼不包含任何東西。 您的var state
為false,因為布爾默認值並執行else子句。
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
}
對於類的字段,布爾值默認值為false。 如果在方法中,則必須按true或false初始化變量。 因此,例如在您的情況下,您將有編譯錯誤。
而且,我並沒有真正理解這一點,但進入if的唯一方法是將條件評估為true。
假設state
在實際代碼中設置了有效的布爾值,則以下條件將成功
if(state)
當state為布爾值時為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
}
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
}
因此,如果您的turnedOn
變量為true, 則if將被調用,if將被賦值為false, 否則將被調用。 如果您不明確指定它們,則將布爾值隱式指定為false eq turnedOn = true
if塊的語法如下,
if(condition){
// Executes when condition evaluates to true.
}
else{
// Executes when condition evaluates to false.
}
在您的情況下,您直接傳遞一個布爾值,因此不需要評估。
假設您要檢查布爾值。 如果是真的,做點什么。 否則,做點別的。 你可以寫:
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{
}
想一想。 你很快就會得到它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.