Results 1 to 7 of 7
  1. #1
    redbrawn06 is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Question 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
    12 heads and 8 tails
    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
    5 heads and 7 tails
    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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.

  3. #3
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default 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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Need help on a program

    Quote Originally Posted by skaterboy987 View Post
    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.

  5. #5
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default 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.

  6. #6
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Re: Need help on a program

    Here's some things that should help you out.

    Java Code:
    scanner.nextLine();
    Gets user input. Example:
    Java Code:
    String Tosses = scanner.nextLine();
    Java Code:
    Integer.parseInt(Tosses);
    Changes the user's input (string) to a integer. Example:
    Java Code:
    int ParseTossworth = Integer.parseInt(Tosses);
    The rest, you'll have to figure out on your own

  7. #7
    redbrawn06 is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Question 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 headcount = 0;
    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 headcount- number of heads resulting
    // 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
    headcount++;// increase int headcount by 1
    }
    else { System.out.print ("T ");// if its anything else it is Tails
    tailcount++;}}

    }}

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •