Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

Java not equals with OR

I'm just wondering why this is not correct :

if ( !name.equals("abc") || !name.equals("cba") )`

And this is correct :

if ((!(name.equals("abc") || name.equals("cba") )))

thank you !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Think about how the ! works.

Take !(a || b)

whats the truth table:

a=f b=f = !(f || f) = !(f) = t
a=f b=t = !(f || t) = !(t) = f
a=t b=f = !(t || f) = !(t) = f
a=t b=t = !(t || t) = !(t) = f

Now take (!a || !b)

a=f b=f = (!f || !f) = (t || t) = t
a=f b=t = (!f || !t) = (t || f) = t
a=t b=f = (!t || !f) = (f || t) = t
a=t b=t = (!t || !t) = (f || f) = f

Now take the correct way to distribute ! (!a && !b)

a=f b=f = (!f && !f) = (t && t) = t
a=f b=t = (!f && !t) = (t && f) = f
a=t b=f = (!t && !f) = (f && t) = f
a=t b=t = (!t && !t) = (f && f) = f

And for completeness sake take the same as (!a || !b) !(a && b)

a=f b=f = !(f && f) = !(f) = t
a=f b=t = !(f && t) = !(f) = t
a=t b=f = !(t && f) = !(f) = t
a=t b=t = !(t && t) = !(t) = f

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...