Results 1 to 5 of 5

Thread: why???

  1. #1
    glina126 is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default 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:
    Still a newbee.. on chapter 7 of 15 in the book "Starting out with Java", by Tony Gaddis. :) (GUIs!!!! :D)

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  3. #3
    glina126 is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default

    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 07:40 AM.
    Still a newbee.. on chapter 7 of 15 in the book "Starting out with Java", by Tony Gaddis. :) (GUIs!!!! :D)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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?

  5. #5
    glina126 is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default

    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!
    Still a newbee.. on chapter 7 of 15 in the book "Starting out with Java", by Tony Gaddis. :) (GUIs!!!! :D)

Posting Permissions

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