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: My program skips an if statement I want to include in my loop--don't know why

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

    Angry My program skips an if statement I want to include in my loop--don't know why

    Hey guys. Bare with me because I am new to Java and am in a class, yet I would love to be great at it at some point. Any advice regarding style, etc. would be great. In this program, I had to make a game where the user bets dollars and guesses if 2 coins land on heads or tails. Everything works, except that in my loop, I want the player to choose whether or not to play again. The program immediately skips to the next part where the player enters his/her guess. Here is the source code and a screen shot of what happens. If you can help me find the issue (which I covered in bold), that would be very helpful as I have been stuck for a while now.

    import java.util.Scanner;
    import java.util.Random;

    public class FlipACoinGame {
    public static void main (String [] args) {

    Scanner reader = new Scanner(System.in);
    Random generator = new Random();

    int coin1, coin2, //two coins
    dollars, //initial number of dollars(input)
    count, //number of tosses to reach depletion
    maxDollars, //maximum amount held by the gambler
    countAtMax, //count when the maximum is achieved
    coin1Guess, coin2Guess; //Player's guess of heads (1) or tails(2)



    //Initialize variables
    countAtMax = 0;
    count = 0;
    maxDollars = 0;

    //Request the input
    System.out.print("How many dollars do you have? ");
    dollars = reader.nextInt();

    //Loop until the money is gone.
    while (dollars > 0){
    count++;


    String playAgain; //Player inputs yes or no for another round

    //Have player input "YES" or "NO" and prediction of heads (1) or tails (2) for each coin.

    System.out.print("Thank you for playing \"Flip A Coin.\" Are you sure you still want to gamble?");
    playAgain = reader.nextLine();

    if (playAgain == "NO"){
    break;
    }


    System.out.print("\nEnter your guess for coin 1 (enter 1 for heads or 2 for tails)");
    coin1Guess = reader.nextInt();
    System.out.print("\nEnter your guess for coin 2 (enter 1 for heads or 2 for tails)");
    coin2Guess = reader.nextInt();

    //Flip coins
    coin1 = generator.nextInt (2) + 1; //1(heads) or 2(tails)
    coin2 = generator.nextInt (2) + 1; //1 (heads) or 2(tails)

    //Calculate winnings and/or losses
    if (coin1 == coin1Guess||coin2 ==coin2Guess){

    dollars += 1;
    System.out.println("ONE MATCH! You win 1 dollar! Your new total is " + dollars + ".");
    }

    else if (coin1 == coin1Guess && coin2 == coin2Guess){
    dollars += 4;
    System.out.println("TWO MATCHES! You win 4 dollars! Your new total is " + dollars + ".");
    }

    else {
    dollars -= 1;
    System.out.println("Sorry, no match. You lose 1 dollar. Your new total is " + dollars + ".");
    }


    //If this is the new maximum, remember it
    if (dollars > maxDollars){
    maxDollars = dollars;
    countAtMax = count;
    }
    }

    //Display results
    System.out.println
    ("You are broke after " + count + " rounds.\n" +
    "You should have quit after " + countAtMax +
    " rolls when you had $" + maxDollars + ".");
    }
    }

    FlipACoinGame.jpg


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,310
    My Mood
    Mellow
    Thanks
    194
    Thanked 669 Times in 656 Posts

    Default Re: My program skips an if statement I want to include in my loop--don't know why

    Welcome!

    Please read the Announcement topic at the top of the sub-forum to learn how to post code correctly along with other useful info for newcomers.

    Do not use '==' to compare String objects. Use the equals() method, e.g., if ( "This".equals( "that" ) ).

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

    Default Re: My program skips an if statement I want to include in my loop--don't know why

    Thank you very much for taking the time to help me. I am not that far in my course with that method, but I now know that I cannot do the same == boolean with String objects. I just told the user to type -1 to quit to keep it to an int and so my program works now. I will definitely check out that topic.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,310
    My Mood
    Mellow
    Thanks
    194
    Thanked 669 Times in 656 Posts

    Default Re: My program skips an if statement I want to include in my loop--don't know why

    You're welcome.

    Some instructors apparently warn their students, "Don't you dare use something we haven't covered in class." If that's your situation, then toe the line. If not, then get into the language and the API that represents the language as deeply as you can, try the things you read about there, and dare to be adventurous and learn stuff on your own. The "I can't use it yet" approach to learning is a bit hard for me to understand and accept.

    But that's me talking. Ignore at will.

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

    Default Re: My program skips an if statement I want to include in my loop--don't know why

    Quote Originally Posted by GregBrannon View Post
    You're welcome.

    Some instructors apparently warn their students, "Don't you dare use something we haven't covered in class." If that's your situation, then toe the line. If not, then get into the language and the API that represents the language as deeply as you can, try the things you read about there, and dare to be adventurous and learn stuff on your own. The "I can't use it yet" approach to learning is a bit hard for me to understand and accept.

    But that's me talking. Ignore at will.
    I don't think my professor is like that. That is very true. Especially with programming, you learn better by experimenting on your own with trial and error. I could not find the announcement page. Can you possibly reply to me with a link to it? Thanks again!

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,310
    My Mood
    Mellow
    Thanks
    194
    Thanked 669 Times in 656 Posts

    Default Re: My program skips an if statement I want to include in my loop--don't know why

    Go here, (the main page of the sub-forum, "What's Wrong With My Code,") and look for the Announcement topic just above the "Post New Thread" button.

Similar Threads

  1. Don't know what statement to put
    By gotdatdough in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 25th, 2013, 09:43 AM
  2. Sqrt Program, skips over method
    By Lashickk in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 21st, 2013, 01:06 PM
  3. [SOLVED] My loop skips index[0] of my array
    By Benner in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2013, 06:09 PM
  4. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 07:15 AM
  5. [SOLVED] New at Java... my if, else if, else program doesn't seem to work, skips to else.Help!
    By KevinE in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 1st, 2010, 03:51 PM