2014-04-02 60 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。

希望这会有所帮助。