Need help on a program

• 10-16-2011, 01:42 AM
redbrawn06
Need help on a program
Hey guys,
I need to write a program that does the following and have no clue how to do so. I'd like to get to where I can do this stuff by myself but cannot figure it out. Please let me know if you can help me out. Here's what the program has to do.
Write a Java program to play a simple game of chance. In this game, the computer will simulate some specified number of coin tosses. All tosses are worth the same absolute dollar value (also specified by the player) but the player must indicate whether he/she wants to play for heads or tails. They will receive a positive dollar value for each toss that matches their choice (i.e., heads or tails) and a negative dollar value for each toss that does not match. Show the results of all tosses. After all tosses have been generated, display the numbers of each and show how much the user has won or lost. Your output should look like the following, simply replacing the numbers in bold with actual user responses and results:

How many tosses shall I make?
20
How much is each toss worth (in dollars)?
5
Do you want to play for heads (H/h) or tails (T/t)?
H
Here are the results:
T H H H T H T T H T H T H H H H T H T H
Congratulations! You won \$20

Here is another sample run:

How many tosses shall I make?
12
How much is each toss worth (in dollars)?
3
Do you want to play for heads (H/h) or tails (T/t)?
H
Here are the results:
H H T H T T H T T T T H
So sorry… You lost \$6

Use KeyboardInputClass.java to get input from the user with the following edit criteria:
number of tosses [1-50]
value of a toss [1-100] (note: integer values only)
play for heads or tails (note: character values H/h or T/t only)

Please let me know if you can help me or tell me how to do this. Thanks so much everybody.
Sincerely,
Redbrawn06
• 10-16-2011, 02:27 AM
Fubarable
Re: Need help on a program
The best help I can give is to tell you to break your big problem down into many smaller and easier to solve steps and then try to solve each step one at a time. For a great site that can help you get started on this, please have a look here: So You Need to Write a Program. Much luck and welcome to the java-forums.org.
• 10-16-2011, 02:37 AM
skaterboy987
Re: Need help on a program
I'll figure out how to do it then respond here. Just give me like 5 minutes or something
• 10-16-2011, 03:27 AM
Fubarable
Re: Need help on a program
Quote:

Originally Posted by skaterboy987
I'll figure out how to do it then respond here. Just give me like 5 minutes or something

All I ask is that you don't do the assignment for him but nudge him so he'll learn to do this himself.
• 10-16-2011, 03:42 AM
skaterboy987
Re: Need help on a program
Okay, I wont directly give the code to him :)
Sorry Fubarable,
anyway, i'm too much of a noob to figure it out myself I had to post my own request for help on this forum.
• 10-16-2011, 06:43 PM
skaterboy987
Re: Need help on a program

Code:

`scanner.nextLine();`
Gets user input. Example:
Code:

`String Tosses = scanner.nextLine();`
Code:

`Integer.parseInt(Tosses);`
Changes the user's input (string) to a integer. Example:
Code:

`int ParseTossworth = Integer.parseInt(Tosses);`
The rest, you'll have to figure out on your own
• 10-16-2011, 10:56 PM
redbrawn06
Re: Need help on a program
Hey everyone thanks so much for your help so far. This is what I've come up with. I'm having a hard time with figuring out a way to compare the face of the coin that the user bets on with the face that is the result of the program. I hope that makes sense. anyway here's what i've got
import java.io.*;
public class Program1 {
public static void main (String []args){
KeyboardInputClass keyboardInput = new KeyboardInputClass();
int tosses;
int tossvalue;
char betface;
int tailcount = 0;
int result;
// int tosses- the number of tosses
//int tossvalue- the value of each toss
//char betface- the face that the user chooses to bet on
// int tailcount- number of tails resulting
//int result- net winning in dollars
tosses= keyboardInput.getInteger (true, 10, 1, 50, "How many tosses shall I make?");
tossvalue= keyboardInput.getInteger (true, 5, 1,100, "How much is each toss worth (in dollars?");
betface= keyboardInput.getCharacter (true, 'H', "TH", 1, "Do you want to play for heads (H/h) or tails (T/t)?");
// uses keyboard input class to get the desired input from the user
for (int i= 0; i < tosses; i++) { //loop beginning dependent on user input
int coin= (int) (Math.random()*12);//Generate random numbers between 0 and 11
if (coin < 6){ System.out.print ("H "); // if the random number generated is less than 6 it is heads