Help with Coin Program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sallyk57
    New Member
    • Oct 2006
    • 13

    #1

    Help with Coin Program

    I have to make a program that would ask a user for their guess (heads or tails) and tells them if its correct or not then it has to ask the user if they want to play again. I figured out how to do this but the part that i am stuck on is how to print a summary at the end of this program tracking how many wins and how many losses.

    import cs1.Keyboard;
    public class CoinToss6
    {
    public static void main(String[] args)
    {
    char play= 'p';
    {
    while (play == 'p' || play == 'P')
    {
    System.out.prin tln(" I'm flipping the coin... Enter heads or tails.");
    String guess= Keyboard.readSt ring();
    Coin myCoin= new Coin();
    myCoin.flip();
    myCoin.toString ();

    if(guess.equals IgnoreCase((myC oin.toString()) ))

    System.out.prin tln("Your guess is correct");
    else
    System.out.prin tln("Your guess is not correct");

    System.out.prin tln("It's " + myCoin.toString ());

    System.out.prin t("If you want to play again press P. Enter anything else if you wish to stop. ");
    play= Keyboard.readCh ar();

    // summary tracking how many wins and how many losses




    }
    }

    }
    }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Outside of the loop, you can define variables such as totalNumOfGames , totalWon, totalLost, etc and set them all to 0. Inside your loop, increment totalNumOfGames , and if they win, increment totalWon, etc. Once your loops exits, you will have all the necessary information to make a summary.

    Comment

    • sallyk57
      New Member
      • Oct 2006
      • 13

      #3
      thank you for the help

      Comment

      • sallyk57
        New Member
        • Oct 2006
        • 13

        #4
        i can't get the totalNumOfGames to work and also it won't print the summary right.

        //coin toss 5 records how many
        import cs1.Keyboard;
        public class CoinToss5
        {
        public static void main(String[] args)
        {
        char play= 'p';
        {
        while (play == 'p' || play == 'P')
        {
        System.out.prin tln(" I'm flipping the coin... Enter heads or tails.");
        String guess= Keyboard.readSt ring();
        Coin myCoin= new Coin();
        myCoin.flip();
        myCoin.toString ();

        if(guess.equals IgnoreCase((myC oin.toString()) ))

        System.out.prin tln("Your guess is correct");
        else
        System.out.prin tln("Your guess is not correct");

        System.out.prin tln("It's " + myCoin.toString ());

        System.out.prin t("If you want to play again press P. Enter anything else if you wish to stop. ");
        play= Keyboard.readCh ar();

        //this is where i changed it
        int totalWon=0, totalLost=0;
        for (int totalNumOfGames =0; totalNumOfGames <totalWon; totalNumOfGames ++)
        {
        myCoin.flip();
        if(guess.equals IgnoreCase((myC oin.toString()) ))
        totalWon++;

        else
        totalLost++;

        }
        System.out.prin tln("The number of games: " +totalNumOfGame s);
        System.out.prin tln("The number of wins: " +totalWon);
        System.out.prin tln("The number of losses "+totalLost );

        }
        }

        }
        }

        Comment

        • DeMan
          Top Contributor
          • Nov 2006
          • 1799

          #5
          your for loop will never execute because you are comparing totalNumofGames (=0) to totalWon(=0) at your exit condition

          I think your counter should be on the previous (while) loop (we use a while loop because we are dealing with an undefined number of iteratons.

          That code is also hard to follow, because it is all over the place consider using the code tags to sperated different pieces of cod
          Code:
          //coin toss 5 records how many 
          import cs1.Keyboard;
          
          public class CoinToss5
          {
            public static void main(String[] args)
            {
            char play= 'p';
            {
            int totalWon=0, totalLost=0;
            while (play == 'p' || play == 'P')
            {
              System.out.println(" I'm flipping the coin... Enter heads or tails.");  
              String guess= Keyboard.readString(); 
              Coin myCoin= new Coin();
              myCoin.flip();
              myCoin.toString();
          
              if(guess.equalsIgnoreCase((myCoin.toString())))
                System.out.println("Your guess is correct"); 
                totalWon++;
              else 
              {
                System.out.println("Your guess is not correct"); 
                System.out.println("It's " + myCoin.toString()); 
                totalLost++
              }
              System.out.print("If you want to play again press P. Enter anything else if you wish to stop. ");
              play= Keyboard.readChar();
            }
          //this is where i changed it 
            System.out.println("The number of games: " +totalNumOfGames);
            System.out.println("The number of wins: " +totalWon);
            System.out.println("The number of losses "+totalLost);
          }

          Comment

          • sallyk57
            New Member
            • Oct 2006
            • 13

            #6
            thanks for the help

            Comment

            Working...