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

Thread: FOR LOOP ISSUSES

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

    Default FOR LOOP ISSUSES

    Im having problems with my for loop. The user has 3 times to roll the number that was guessed. when the condition is met on the the third try I want it to say that they won not that they lose
    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());
    }


  2. #2
    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: FOR LOOP ISSUSES

    I'm not seeing any problem here. If the getAnswer() == guess then you get your win condition. If that is not happening i would look to see if getAnswer() and guess are getting "true" from that condition when they are actually equal. Depending upon what type getAnswer() and guess are you may need to use .equals(guess) instead off the == operator.

    Can you show more code? Can you show what your input/output is? Any errors displaying?

    also put your code in code tags please!
    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!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: FOR LOOP ISSUSES

    You need to post enough code to show the definitions for the variables used in the code.
    If the code is comparing String objects, it should use the equals() method not the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts
    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!

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: FOR LOOP ISSUSES

    Please read the forum rules and don't double post. Thread locked.

Similar Threads

  1. help with when the for loop is met and i want to run the while loop again
    By m49er704 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 22nd, 2013, 09:03 AM
  2. 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, 09:00 PM
  3. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 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