1. Member Join Date
Feb 2011
Posts
28
Rep Power
0 why???

why doesnt this code run!?

Java Code:
while ((randomNumber1 != userNumber1 && randomNumber2 != userNumber2 && randomNumber3 != userNumber3 && randomNumber4 != userNumber4));

i wrote a simple mastermind game and once the numbers match i want the do loop to stop. it stops even if only one of the numbers match :confused:  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
17 it stops even if only one of the numbers match

Suppose we have

Java Code:
randomNumber1 is 1     userNumber1 is 100
randomNumber2 is 2     userNumber1 is 200
randomNumber3 is 3     userNumber1 is 3
randomNumber4 is 4     userNumber1 is 400

(ie only one match)

Think about the bits that make up the condition:

Java Code:
randomNumber1 != userNumber1  is  true
randomNumber2 != userNumber2  is  true
randomNumber3 != userNumber3  is  false
randomNumber4 != userNumber4  is  true

So the combined condition is true&&true&&false&&true which is, of course, false. Hence the loop stops.  Reply With Quote

3. Member Join Date
Feb 2011
Posts
28
Rep Power
0 well then why does it still stop if i have this code?

Java Code:
while ((randomNumber1 == userNumber1 && randomNumber2 == userNumber2 && randomNumber3 == userNumber3 && randomNumber4 == userNumber4));

i just did a visualization for myself.

false
false
true
false

if following your example. how can i write this then???
Last edited by glina126; 02-25-2011 at 06:40 AM.  Reply With Quote

4. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
17 Same thing.

Consider the values as before where there is only one match and break your condition up into its four components. What four truth values do you get for them?  Reply With Quote

5. Member Join Date
Feb 2011
Posts
28
Rep Power
0 I did this and it works :D thanks so much

Java Code:
x++;

if (randomNumber1 == userNumber1 && randomNumber2 == userNumber2 && randomNumber3 == userNumber3 && randomNumber4 == userNumber4)
{
x = 8;
}
}
while (x <= 7);
no more late night programming for me. period. thanks a bunch!  Reply With Quote Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•