Reputation: 3
is this wrong? I want to exit the while loop when the Ran1,Ran2,Ran3 are all the same. tnx
Random RandomNumber = new Random();
int Ran1 = RandomNumber.nextInt(4);
int Ran2 = RandomNumber.nextInt(4);
int Ran3 = RandomNumber.nextInt(4);
while (Ran1 != Ran2 && Ran1 != Ran3 && Ran2 != Ran3 ){
Ran1 = RandomNumber.nextInt(4);
Ran2 = RandomNumber.nextInt(4);
Ran3 = RandomNumber.nextInt(4);
System.out.print(Ran1);
System.out.print(" ");
System.out.print(Ran2);
System.out.print(" ");
System.out.println(Ran3);
}
Upvotes: 0
Views: 3902
Reputation: 1586
You want to keep iterating while the 3 values are different each other, so the way to express the condition is
while(Ran1 != Ran2 || Ran2 != Ran3 )
Because implicitly it means that Ran1 and Ran3 are equal once you have compared Ran2 with the other ones
BTW, you can simplify your
System.out.print(Ran1);
System.out.print(" ");
System.out.print(Ran2);
System.out.print(" ");
System.out.println(Ran3);
By using a single:
System.out.println(Ran1 + " " Ran2 + " " + Ran3);
Upvotes: 0
Reputation: 229321
This is just simple boolean logic. You can try it yourself. Let's say Ran1
is 3, Ran2
is 4, and Ran3
is 3. What would the expression evaluate to?
Ran1 != Ran2 -> 3 != 4 -> True
Ran1 != Ran3 -> 3 != 3 -> False
Ran2 != Ran3 -> 4 != 3 -> True
Those are not all True, so AND
ing them together is False, and your loop would break. But that's not what you want, you want it to only be False when they are all the same. If you notice, the way you have it now, the loop breaks if any one of the three variables equals any other one.
It's easier to think in terms of positives instead of double negatives. When are all the variables the same? It's when Ran1 == Ran2
, Ran1 == Ran3
, and Ran2 == Ran3
, or, in Java syntax:
Ran1 == Ran2 && Ran1 == Ran3 && Ran2 == Ran3
Now, as long as this is not true, you want your loop to continue, so make sure to wrap it in parenthesis and use the NOT
operator !
:
while (!(Ran1 == Ran2 && Ran1 == Ran3 && Ran2 == Ran3)) {
...
}
That's it, but if you don't want to use parentheses then you can simply the expression using boolean logic. !(A && B)
is the same as !A || !B
:
A | B | A && B | ! (A && B) | !A | !B | !A || !B
---+-----+----------+--------------+-----+------+-----------
T | T | T | F | F | F | F
T | F | F | T | F | T | T
F | T | F | T | T | F | T
F | F | F | T | T | T | T
You can verify for yourself that !(A && B && C)
is the same as !A || !B || !C
. Thus you can turn the above while
loop into:
while (!(Ran1 == Ran2) || !(Ran1 == Ran3) || !(Ran2 == Ran3)) {
...
}
or:
while (Ran1 != Ran2 || Ran1 != Ran3 || Ran2 != Ran3) {
...
}
In terms of reasoning about it with words: you want the loop to continue so long as any pair of the three variables aren't equal. Your initial expression Ran1 != Ran2 && Ran1 != Ran3 && Ran2 != Ran3
would only continue the loop so long as all pairs of the three variables aren't equal.
Upvotes: 6
Reputation: 12474
Logically yours is fine, but it can be a little shorter/easier to read. You want to use a NOT operator ( !
) in the condition, because that's how you think of it:
while ( ! ( ( Ran1 == Ran2) && (Ran1 == Ran3) ) )
Upvotes: 0
Reputation: 133567
You want to exit the loop when they are all the same. Since equality is transitive, you know that if A == B && B == C
then A == C
. This means that you can exit just when
Ran1 == Ran2 && Ran1 == Ran3
so you must keep inside the loop until the negation of the previous condition is true:
! (Ran1 == Ran2 && Ran1 == Ran3)
which is equivalent, by using De Morgan's law, to:
Ran1 != Ran2 || Ran1 != Ran3
Upvotes: 4