繁体   English   中英

简化布尔表达式示例

[英]Simplify Boolean Expression example

如何简化

if ( this.something == false ) 

这个布尔表达式? 其实我想问的是什么是Simplify Boolean Expression

你可以简单地做:

if (!this.something)

您可以直接使用布尔变量:

例子:

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

使用如下,因为 if 表达式需要一个布尔值。

if (!this.something) {
//
}

您可以使用三元运算符进行更简化:

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

如果this.somethingfalse someVariable将为 1 。

希望这可以帮助。

简化布尔表达式就是降低表达式的复杂度,同时保留其含义。

在你的情况下:

if(!this.something)

具有相同的含义,但要短一些。

为了简化更复杂的示例,您可以使用真值表或卡诺图。

通常 if 语句想要将其中的任何内容评估为布尔值 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)

在某些情况下,您还可以通过使用三元运算符来简化 if 语句:

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM