Java statement to check if any conditions are false, but not both or none

Is there a statement in Java that will return false if both conditions are false, but if both are true or both are false, will the result be true?

I have code that relies on a user entering some values ​​to start a process. Since the user should only be able to enter x or y, but not both, or not, I would like to show an error message in this case.

+6
java logic
source share
1 answer

Do you want XNOR, basically:

if (!(a ^ b)) 

or (easier)

 if (a == b) 

where a and b are conditions.

Code example:

 public class Test { public static void main(String[] args) { xnor(false, false); xnor(false, true); xnor(true, false); xnor(true, true); } private static void xnor(boolean a, boolean b) { System.out.printf("xnor(%b, %b) = %b\n", a, b, a == b); } } 

Produces this truth table;

 xnor(false, false) = true xnor(false, true) = false xnor(true, false) = false xnor(true, true) = true 
+20
source share

All Articles