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 10 of 10

Thread: help with when the for loop is met and i want to run the while loop again

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default help with when the for loop is met and i want to run the while loop again

    when the guess is the same as the dice roll and i enter yes again it only lets me enter the guess. then its asks me if i want to go again instead of running three rolls

    --- Update ---

    import javax.swing.JOptionPane;

    public class Guess
    {
    public static void main(String args[])
    {
    int guess = 0;
    int roll1 = 0;
    int roll2 = 0;
    String goAgain;



    Roll game1 = new Roll(guess, roll1, roll2);
    boolean hasWon = false;

    do
    {

    guess = Integer.parseInt(JOptionPane.showInputDialog("Ente r the number that you hope to roll"));
    JOptionPane.showMessageDialog(null, "Your guess was: " + guess);
    game1.setDice1(roll1);
    game1.setDice2(roll2);


    for(int x = 1; x <= 3 && hasWon == false; x++)
    {
    if(game1.getAnswer() == guess)
    {

    JOptionPane.showMessageDialog(null, guess + "and " + game1.getAnswer() + " You WIN!!");
    hasWon = true;
    }
    else
    {
    game1.setDice1(roll1);
    game1.setDice2(roll2);
    JOptionPane.showMessageDialog(null, game1.toString());

    }
    if(x == 3)
    {
    JOptionPane.showMessageDialog(null, "you lose");

    }

    }
    goAgain = JOptionPane.showInputDialog("Would you like to play again?");



    }while(goAgain.equalsIgnoreCase("Yes"));

    }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: help with when the for loop is met and i want to run the while loop again

    :lightbulb:

    I just realised you never reset hasWon boolean.

    --- Update ---

    WOW!

    I really hate this new update feature.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: help with when the for loop is met and i want to run the while loop again

    well I put

    hasWon = false; in the else statement

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: help with when the for loop is met and i want to run the while loop again

    Did that work?
    Do you know what I mean by resetting the boolean?
    If the user wins the boolean becomes true, will the for loop enter the second time around?
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: help with when the for loop is met and i want to run the while loop again

    no it didn't work
    do you mean turning it from true back to false?
    that's what it seems like its doing. when the user win it will not enter the for loop again?
    im just confused on how to fix that

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: help with when the for loop is met and i want to run the while loop again

    Each time the outer loop runs you want the boolean to be false otherwise it will never enter the inner for loop. Therefore as the first line of the do/while loop and a line of code to reset the boolean back to false.

    Another issue I mentioned earlier but got deleted: what will happen if the user wins on the third roll? It will print "You win" and then "you lose"
    Improving the world one idiot at a time!

  7. The Following User Says Thank You to Junky For This Useful Post:

    m49er704 (March 22nd, 2013)

  8. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: help with when the for loop is met and i want to run the while loop again

    Oh ok I understand.
    Yes I encountered that. Well I can't say if x > 3 because the loop only runs 3 times ?
    I tried if(guess != getANswer()
    { "you lose"}
    But that will tell you that you lose no matter what

  9. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: help with when the for loop is met and i want to run the while loop again

    Instead of having the x==3 situation as a separate if statement, try making it another condition of your if statement as an else if.
    Improving the world one idiot at a time!

  10. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: help with when the for loop is met and i want to run the while loop again

    still having some trouble with the x = 3 in the loop. heres how it looks now

    for(int x = 0; x < 3 && hasWon == false; x++)
    {
    if(game1.getAnswer() == guess)
    {

    JOptionPane.showMessageDialog(null, guess + "and " + game1.getAnswer() + " You WIN!!");
    hasWon = true;
    }
    else if(x == 3 && game1.getAnswer() != guess)
    {
    JOptionPane.showMessageDialog(null, "YOU lose!!");
    }
    else
    {
    game1.setDice1(roll1);
    game1.setDice2(roll2);
    JOptionPane.showMessageDialog(null, game1.toString());
    }

  11. #10
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: help with when the for loop is met and i want to run the while loop again

    How is x ever going to equal 3 when your for loop breaks on x<3? Start there.

    What is the undesired effect you are getting right now? That would help us hunt down your problem.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate 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. My while (true) loop method only seems to run once!
    By ajfonty in forum Object Oriented Programming
    Replies: 2
    Last Post: May 18th, 2012, 08:46 PM
  3. [SOLVED] Beginner, stuck on implementing while loop, compiles fine but still won't run
    By GregC in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 1st, 2012, 06:36 PM
  4. 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
  5. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM