newbie here,
I have two variables which generate random numbers through .Random
. I want them keep rolling until both variables generate two different values, si
Your logic is incorrect. The loop will continue as long as both values don't match - as soon as one value matches, the loop exits. We can invert your logic to show this:
while (!(diceRolled1 == 5 || diceRolled2 == 4)) {
which is logically equivalent to what you have.
What you want is this:
while (diceRolled1 != 5 || diceRolled2 != 4) {
which says "Continue while any variable does not have the desired value"
the while execute the statement untill the condition is true. In your code the condition is given by (diceRolled1 != 5) && (diceRolled2 != 4). The && operator require true that all operands be true. Given this Your loop will end when at least one of the expression will be false.
To finish the program when it generate 5 and 4 you have to use this:
(!(diceRolled1 == 5) && (diceRolled2 == 4))
You're getting the logical result you describe, but it wasn't what you expect. Specifically, when either of your conditions evaluates to false
the logical and will not evaluate to true
. I think you wanted
while (!(diceRolled1 == 5 && diceRolled2 == 4)) {
which is while
not dice1 equal to 5 and dice2 equal to 4. And then, using De Morgan's Laws that might also be expressed as
while (diceRolled1 != 5 || diceRolled2 != 4) {
which means loop while
dice1 is not equal to 5 or dice2 is not equal to 4.
Yeah,it should be. The program should end if dicerolled is either 5 or 4 because as far as it is not 4 and not 5 it is in while loop. It exits the while loop if only the value is either 4 or 5. So your logic is incorrect. Sorry! :) Try:
while (!(dicerolled ==4 && dicerolled == 5))