Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Less than two variables

Tags:

java

I have tried to search as much as I can but it's kinda hard when you are a beginner. Anyway im trying to learn Java and im stuck on a question that says "var1 can be bigger than var2 or var3 but not both. And im supposed to answer with a Boolean value.

e = var1 > var2 || var1 > var3 && var1 < var2 + var3;

If I do this I end up with it just doing a total sum of var2 and var3. How do I do like

var1 < var2 AND var3;
like image 380
John Avatar asked Feb 14 '26 15:02

John


2 Answers

You can use the exclusive-OR operator:

(var1 > var2) ^ (var1 > var3)

A ^ B is true if and only if either A or B (but not both) are true:

A  B  A^B
---------
0  0   0
0  1   1
1  0   1
1  1   0
like image 56
arshajii Avatar answered Feb 16 '26 05:02

arshajii


If var1 "can" be bigger than var2, and "can" be bigger than var3, but not both, the answer is pretty simple.

boolean response = !(var1>var2 && var1>var3)
boolean response = (var1<=var2 || var1<=var3)
like image 39
Fabinout Avatar answered Feb 16 '26 04:02

Fabinout



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!