473,472 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help with Coin Program

13 New Member
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
5 3826
Ganon11
3,652 Recognized Expert Specialist
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
sallyk57
13 New Member
thank you for the help
Dec 8 '06 #3
sallyk57
13 New Member
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
1,806 Top Contributor
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
sallyk57
13 New Member
thanks for the help
Dec 9 '06 #6

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

Similar topics

0
by: Jon Monteleone | last post by:
Greetings, I posted a few days back and didnt get much of a response, so I figured I would post again with more detail. I am running gnome under fedora core 4. I want a kid to be able to drop a...
2
by: atreide_son | last post by:
hello all... yes i'm a newbie and i need help. i have an assignment due for class, but I don't know i'm stuck and can't get out from under myself. here's the focus of the program: Write...
3
by: bb91915 | last post by:
I am taking a beginners class in C and am attempting to write code for extra problems my instructor gave us. These are not for grade and are only used solely for practice. I have a hard time...
12
by: DannyB | last post by:
I'm just learning Python. I've created a simple coin flipper program - here is the code: #Coin flipper import random heads = 0 tails = 0 counter = 0
52
by: celerysoup16 | last post by:
I've written this coin toss program, and can't figure out why it isn't giving accurate results... cheers, Ben #include <stdlib.h> #include <stdio.h> #define H 1 #define T 0 #define SENTINEL...
4
by: sallyk57 | last post by:
I figured out how to make a coin class. But i can't figure out how to write a program to find the length of the longest run of heads in 100 flips of the coin. This is what i did so far the runs...
3
by: noellieb | last post by:
I have class project and was asked to design 2 classes. 1 : Class Coin designated by coin type (PENNY, NICKEL, DIME, QUARTER OR DOLLAR) 2: Create Class CoinChanger To allow coin changing : say if...
1
by: laila2ethan | last post by:
Need someones help , I am having a very difficult time understanding parameters and functions, can someone help me answer these questions 1. Write a program that helps an elementary school student...
0
Chuncatty
by: Chuncatty | last post by:
How much does a Draped Bust coin fabricate cost? (Sorry for my Eng) I ask a beau to see if it's genuine he said it is, down repay a collage professor i met said so. It's in virtue word,...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.