Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Trouble with my while loop

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trouble with my while loop

    I been trying to create a Pig game to help keep me refresh in Java code, and Im having trouble with my while loop not ending when it should.

    while (pTotal < 100 || cTotal < 100)
    			{
    				rollDice = "roll";
    				roll = true;
    				while (rollDice.equalsIgnoreCase("roll"))
    				{
    					dice.roll ();
     
    					System.out.println ("You rolled " + dice.getValue1 () + " and " + dice.getValue2() + ".");
     
    					if (dice.check())
    					{
    						if (dice.getValue1() == 1)
    							{
    								pTotal = 0;
    								rollDice = "pass";
    								roundTotal = 0;
    								roll = false;
    							}
    					}
    					if (!dice.check () && dice.getValue1 () == 1 || dice.getValue2() == 1)
    					{
    						roundTotal = 0;
    						rollDice = "pass";
    						roll = false;
    					}
     
    					if (roll)
    					{
    						roundTotal += dice.getValue1 () + dice.getValue2 ();
    						System.out.println (roundTotal);
    						System.out.println ("Would you like to roll again or pass the dice?" +
    								"(roll/pass): ");
    						rollDice = scan.next ();
    					}
    					if (rollDice.equalsIgnoreCase("pass"))
    					{
    						pTotal += roundTotal;
    						roundTotal = 0;
    					}
    				}
    				rollDice = "roll";
    				roll = true;
    				while (rollDice.equalsIgnoreCase("roll") && pTotal < 100)
    				{
    					dice.roll ();
     
    					if (dice.check())
    					{
    						if (dice.getValue1() == 1)
    						{
    								cTotal = 0;
    								rollDice = "pass";
    								roundTotal = 0;
    								roll = false;
    						}
    					}
    					if (!dice.check () && dice.getValue1 () == 1 || dice.getValue2() == 1)
    					{
    						roundTotal = 0;
    						rollDice = "pass";
    						roll = false;
    					}
    					if (roll)
    					{
    						roundTotal = dice.getValue1 () + dice.getValue2 ();
    					}
    					if (roundTotal >= 20)
    					{
    						cTotal += roundTotal;
    						rollDice = "pass";
    					}
    				}
    				System.out.println ("Computer Total: " + cTotal);
    				System.out.println ("Player total: " + pTotal);

    The methods in my PairOfDice class are as follows:

    check, which returns true if the face value of both dice is the same, false otherwise
    roll, which rolls both the dice to a new value between 1 and 6
    .getValue1 and getValue2 return the face value of the first die and the face value of the 2nd die

    If I done this correctly, my loop is suppose to end once either the player or computer reach 100 and print the results on screen, but it continues on even after either reaches 100. Can anyone spot the error in this loop as I'm having trouble spotting any flaws in my logic.


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Trouble with my while loop

    Shouldn't it be AND in place of OR
    while (pTotal < 100 && cTotal < 100)

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with my while loop

    Quote Originally Posted by ilan View Post
    Shouldn't it be AND in place of OR
    while (pTotal < 100 && cTotal < 100)
    The idea behind the game is for it to terminate once either the player OR the computer reaches 100 points, not both reach 100.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: Trouble with my while loop

    I'd recommend posting an SSCCE. There are a lot of references to variables and classes that are not defined in the code you posted, leaving us guessing as to what they.

  5. #5
    Junior Member
    Join Date
    Oct 2009
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Trouble with my while loop

    masterhand,
    ilan is correct. What you have written is for the while loop to run if at least one of them is under 100.
    You want it to terminate when one goes over, so you check if both are under 100.

  6. #6
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with my while loop

    I see what he meant now, thank you. x,x

    @copeg, sorry for not trying that instead. I was just thinking at the time show the whole thing and see if someone could possibly help me.

    Anyway, Ill called this solved now cause that looks like my problem with it.

Similar Threads

  1. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 10:00 PM
  2. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 01:15 PM
  3. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  4. Trouble Creating Instances with a While Loop
    By AbsolutelyNobody in forum Loops & Control Statements
    Replies: 3
    Last Post: October 13th, 2011, 08:20 AM
  5. [SOLVED] Trouble with draw and fillRect in pyramid logic using nested loop
    By LiquidMetal in forum Loops & Control Statements
    Replies: 4
    Last Post: April 27th, 2009, 04:25 AM