java 更好的方法来确定两个变量是真还是假

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5455348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:12:31  来源:igfitidea点击:

Better ways to find if both variables are true or both are false

javaboolean-logic

提问by user679737

I have two variables which can be either true or false. I get these by doing query on database for presence or absence of certain product ids.

我有两个变量,可以是真也可以是假。我通过在数据库上查询某些产品 ID 的存在或不存在来获得这些。

Now I need to set another variable which will be true or false. it will be true value when both the variables are true or both are false. It will be false value of one is true and other is false.

现在我需要设置另一个为真或假的变量。当两个变量都为真或都为假时,它将是真值。一个是真的,另一个是假的,这将是假的。

at present I take care of it using if statement

目前我使用 if 语句来处理它

if ( v1 == true && v2 == true )
 result = true;
else if ( v1==false && v2 == false )
 result = true;
else if ( v1 == true && v2 == false )
 result = false;
else if ( v1==false && v2 == true )
 result = false;

Is there exists a better way of doing this ?

有没有更好的方法来做到这一点?

回答by DXM

I may be missing something very fundamental, but I'll give it a go:

我可能会遗漏一些非常基本的东西,但我会试一试:

result = ( v1 == v2 );

回答by codaddict

You can use the logical XOR operator and logical NOT operator as:

您可以将逻辑 XOR 运算符和逻辑 NOT 运算符用作:

result = !(v1^v2);

回答by Jerry Asher

This sort of problem, given a truth table, minimize the logic required to reproduce the truth values, is often nicely treated, with Karnaugh Maps

这类问题,给定真值表,最小化重现真值所需的逻辑,通常可以很好地处理,使用卡诺图

Your truth table here looks like:

您的真值表如下所示:

 v1 v2  f(v1, v2)
  t  t     t
  t  f     f
  f  t     f
  f  f     t

And actually, as others have noted, given that truth table, a basic familiarity with logic should right away yield the function !xor

实际上,正如其他人所指出的,鉴于该真值表,对逻辑的基本熟悉应该立即产生该函数!异或

However, if you take the truth table and draw it as a Karnaugh Map, it looks like:

然而,如果你把真值表画成卡诺图,它看起来像:

        v2
       f   t 
     ---------
 v  f| t | f |
 1  t| f | t |
     ---------

And the function looks like: !v1!v2 || v1v2 which if you look at 2 variable karnaugh map examplesagain can be seen to simplify to ! xor

函数看起来像: !v1!v2 || 如果您再次查看2 个变量卡诺图示例,可以看到v1v2简化为 !异或

Admittedly, 2 variable karnaugh maps are probably best treated with the ordinary logical operations by well, familiarity and memorization. But when expanding beyond 2 variables, Karnaugh maps are very illuminating -- you should look into them.

诚然,2 变量卡诺图可能最好通过普通的逻辑运算通过良好、熟悉和记忆来处理。但是当扩展超过 2 个变量时,卡诺图非常有启发性——你应该研究它们。

回答by Rune Aamodt

Use the XOR operator (^):

使用 XOR 运算符 (^):

boolean result = !(v1 ^ v2)

回答by Troydm

if(v1 == v2) 
return true; 
else 
return false;

回答by sreekanth

The simple way is,if the two variables are equal then it should be true and if any one is false it is false. check this one.

简单的方法是,如果两个变量相等,则它应该为真,如果任何一个为假,则为假。检查这个。

if(v1 == v2)
    return true;
else 
    return false;

回答by vegasaurus

Why not just compare the two?

为什么不直接比较两者呢?

if(v1 == v2) result = true;