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;
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With