检查相等和不相等条件是否为双精度值 [英] check Equal and Not Equal conditons for double values

查看:85
本文介绍了检查相等和不相等条件是否为双精度值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 == !=比较两个双重值时,我遇到了困难!

I am facing difficulty in comparing two double values using == and !=.

我创建了6个double变量,并尝试在 If 条件下进行比较。

I have created 6 double variables and trying to compare in If condition.

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
}

尽管我的条件是 a和b相等控制权将转至,如果部分

Though my condition is a and b are equal control is going to else if part

所以我尝试了 a。 equals(b)等同条件,对我来说很好。

So I have tried a.equals(b) for equal condition, Which is working fine for me.

我的查询是如何检查 a不等于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.

推荐答案

如果您使用的是 double (原始类型),则 a 并且 b 不能相等。

If you're using a double (the primitive type) then a and b must not be equal.

double a = 1.0;
double b = 1.0;
System.out.println(a == b);

如果 .equals()有效,可能使用的对象包装类型为 Double 。此外,与!= .equals()等效的是

If .equals() works you're probably using the object wrapper type Double. Also, the equivalent of != with .equals() is

!a.equals(b)

编辑

此外,

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 and test again,

 else if (!(a==b||c==d||e==f))

或使用德摩根定律

 else if (a != b && c != d && e != f)

这篇关于检查相等和不相等条件是否为双精度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆