2014-04-02 159 views
-4

如何簡化簡化布爾表達式示例

if (this.something == false) 

此布爾表達式? 其實我想問的是什麼簡化布爾表達式

+2

'if(!this.something)'? – assylias

+5

可以閱讀一兩本書。 – HamZa

回答

5

你可以簡單地做:

if (!this.something) 

您可以直接使用布爾變量:

例子:

boolean flag = true; 
if(flag) { 
    //do something 
} 
1

使用像,因爲如果需要表達一個布爾值以下。

if (!this.something) { 
// 
} 
0

簡化布爾表達式是爲了減少表達式的複雜性,同時保留含義。

你的情況:

if(!this.something) 

具有相同的含義,但它是一個有點短。

爲了簡化更復雜的例子,您可以使用真值表或卡諾圖。

0

一般的if語句要評估什麼是在它爲布爾 如果

boolean something = true; 
if(something == true) evaluates to true 
if(something != true) evaluates to false 

,但你也可以做

if(something) evaluates to whatever something is (true in this case) 
if(!something) evaluates to opposite what something is (false in this example) 

您也可以簡化,如果在某些情況下語句使用三元運營商:

boolean isSomethingTrue = (something == true) ? true : false; 
boolean isSomethingTrue = something ? true : false; 
type variable = (condition) ? return this value if condition met : return this value if condition is not met; 
0

你可以我們e三元運算符,以獲得更多簡化:

int someVariable = (this.something)?0:1; 

如果this.something爲false,那麼someVariable將爲1。

希望這會有所幫助。