Java 检查双值的等于和不等于条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24902801/
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
check Equal and Not Equal conditons for double values
提问by Siva
I am facing difficulty in comparing two double values
using ==
and !=
.
我在比较两个double values
使用==
and时遇到困难!=
。
I have created 6 double variables and trying to compare in If
condition.
我创建了 6 个双变量并尝试比较If
条件。
double a,b,c,d,e,f;
if((a==b||c==d||e==f))
{
//My code here in case of true condition
}
else if ((a!=b||c!=d||e!=f))
{
//My code here in case false condition
}
Though my condition is a and b are equal
control is going to else if
part
虽然我的情况是a and b are equal
控制将要else if
分开
So I have tried a.equals(b)
for equal condition, Which is working fine for me.
所以我尝试a.equals(b)
了同等条件,这对我来说很好。
My query here is how can I check a not equal b
. I have searched the web but I found only to use !=
but somehow this is not working for me.
我的查询是如何检查a not equal b
. 我在网上搜索过,但我发现只能使用,!=
但不知何故这对我不起作用。
采纳答案by Elliott Frisch
If you're using a double
(the primitive type) then a
and b
must not be equal.
如果您使用的是double
(原始类型),则a
andb
不能相等。
double a = 1.0;
double b = 1.0;
System.out.println(a == b);
If .equals()
works you're probably using the object wrapper type Double
. Also, the equivalent of !=
with .equals()
is
如果.equals()
可行,您可能正在使用对象包装器类型Double
。此外,!=
与 with的等价物.equals()
是
!a.equals(b)
Edit
编辑
Also,
还,
else if ((a!=b||c!=d||e!=f))
{
//My code here in case false condition
}
(Unless I'm missing something) should just be
(除非我错过了什么)应该只是
else
{
//My code here in case false condition
}
if you really want invert your test conditions andtest again,
如果你真的想反转你的测试条件并再次测试,
else if (!(a==b||c==d||e==f))
Or use De Morgan's Laws
或使用德摩根定律
else if (a != b && c != d && e != f)
回答by Deepanshu J bedi
You can use
您可以使用
!a.equals(b) || !c.equals(d) || !e.equals(f)
Well with double data type you can use
您可以使用双数据类型
==,!= (you should try to use these first.. )