By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,779 Members | 1,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,779 IT Pros & Developers. It's quick & easy.

Help with Coin Program

P: 13
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.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");
else
System.out.println("Your guess is not correct");

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

System.out.print("If you want to play again press P. Enter anything else if you wish to stop. ");
play= Keyboard.readChar();

// summary tracking how many wins and how many losses




}
}

}
}
Dec 8 '06 #1
Share this Question
Share on Google+
5 Replies


Ganon11
Expert 2.5K+
P: 3,652
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.
Dec 8 '06 #2

P: 13
thank you for the help
Dec 8 '06 #3

P: 13
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.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");
else
System.out.println("Your guess is not correct");

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

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
int totalWon=0, totalLost=0;
for (int totalNumOfGames=0; totalNumOfGames<totalWon; totalNumOfGames++)
{
myCoin.flip();
if(guess.equalsIgnoreCase((myCoin.toString())))
totalWon++;

else
totalLost++;

}
System.out.println("The number of games: " +totalNumOfGames);
System.out.println("The number of wins: " +totalWon);
System.out.println("The number of losses "+totalLost);

}
}

}
}
Dec 8 '06 #4

DeMan
100+
P: 1,806
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
Expand|Select|Wrap|Line Numbers
  1. //coin toss 5 records how many 
  2. import cs1.Keyboard;
  3.  
  4. public class CoinToss5
  5. {
  6.   public static void main(String[] args)
  7.   {
  8.   char play= 'p';
  9.   {
  10.   int totalWon=0, totalLost=0;
  11.   while (play == 'p' || play == 'P')
  12.   {
  13.     System.out.println(" I'm flipping the coin... Enter heads or tails.");  
  14.     String guess= Keyboard.readString(); 
  15.     Coin myCoin= new Coin();
  16.     myCoin.flip();
  17.     myCoin.toString();
  18.  
  19.     if(guess.equalsIgnoreCase((myCoin.toString())))
  20.       System.out.println("Your guess is correct"); 
  21.       totalWon++;
  22.     else 
  23.     {
  24.       System.out.println("Your guess is not correct"); 
  25.       System.out.println("It's " + myCoin.toString()); 
  26.       totalLost++
  27.     }
  28.     System.out.print("If you want to play again press P. Enter anything else if you wish to stop. ");
  29.     play= Keyboard.readChar();
  30.   }
  31. //this is where i changed it 
  32.   System.out.println("The number of games: " +totalNumOfGames);
  33.   System.out.println("The number of wins: " +totalWon);
  34.   System.out.println("The number of losses "+totalLost);
  35. }
  36.  
Dec 9 '06 #5

P: 13
thanks for the help
Dec 9 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.