簡體   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