473,831 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ 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.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




}
}

}
}
Dec 8 '06 #1
5 3847
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.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 );

}
}

}
}
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
1681
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 quarter into a coin slot and get 15 minutes of time on an account that has "restricted" Internet access via the firefox web browser. We are experimenting with different ways to allow kids to safely surf the web during school hours while...
2
4684
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 a C program that allows the user to make some simple banking
3
1928
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 taking pseudocode and turning it into code. Since I am in a beginners class, we have only learned so much to date, so what I am presenting will be really rough, so please pardon my ignorance. Here are my instructions, with my code to follow: ...
12
2862
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
5414
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 -1
4
3929
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 program. Thanks // this is the coin class import java.util.Random; public class Coin { private int face; private final int HEADS=0; private final int TAILS=1;
3
1414
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 one were to buy something to return the correct change. I have the following so far. Please Help!!! This is my class COIN : public enum Coin { penny(1, "penny"), nickel(5,"nickel"), dime(10,"dime"), quarter(25,"quarter"); ...
1
2837
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 learn multiplication. Use rand to produce two positive one-digit integers. It should then type a question such as How much is 6 times 7? The student then types the answer. Your program checks the student's answer. If it is correct,...
0
1173
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, well-grounded the top formerly larboard piece is kinda scratched. How much do you meditate on it's worth? pic of coin is here: http://coindad.com/coin/Cent_1803_F.jpg" http://coindad.com/coin/Cent_1803_F.jpg
0
9642
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10777
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10494
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10208
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9317
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7748
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5620
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5785
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.