Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Flipping a coin

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Flipping a coin

    So I am in a CIS 111 course and I cannot figure out how to do this program. I am at the point now where I can read java and understand exactly what the program will do but still being able to think of what class/methods to call still boggles my mind. What I am trying to do in this program is this.

    1. Flip the coin 100 times
    2. I want it to keep track of the highest amount of heads flips in a row.
    3. I want it to keep track of the highest amount of tails flips in a row.

    at the end of the program I want it to print out the highest amount of heads flips in a row and tails flips in a row. I'm honestly not sure how to achieve this with the program I currently have altered. I could really use some help here and I figure someone here could help a noobie out .
    public class Runs
    {
     
    public static void main (String[] args)
    {
     
    final int FLIPS = 100; // number of coin flips
    int currentRun = 0; // length of the current run of HEADS
    int currentRunTails = 1;
    int maxRunTails = 1;
    int maxRunHeads = 0; // length of the maximum run so far
    int lastflip; // I need the last flip before current flip then set current flip equal to last flip.
    int currentflip;
     
    // Create a coin object
    Coin myCoin = new Coin(); // creates coin object
     
    // Flip the coin FLIPS times
    for (int i = 0; i <= FLIPS; i++) //
    {
    // Flip the coin & print the result
    myCoin.flip();
    System.out.println(myCoin);
    currentflip = ;
    lastflip = currentflip;
     
    // Update the run information
    if (myCoin.getFace() == 0)
    {
    currentRun++;
    // currentRunTails++; // If we keep getting heads in a row it will add 1 to each time before we got heads.
    if (currentRun > maxRunHeads)
    maxRunHeads = currentRun;
    // currentRun = 0;
    }//ends the if
    else //we just threw a TAILS
    currentRunTails++;
    if (currentRunTails > maxRunTails)
    maxRunTails = currentRunTails;
    }//ends the for
    // Print the results
    System.out.println("The highest amount of HEADS in a row we have gotten is: " + maxRunHeads);
    System.out.println("The highest amount of TAILS in a row we have gotten is: " + maxRunTails);
    }//ends the main
    }// ends the runs class
    Last edited by jimboslice; October 17th, 2010 at 06:39 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,252
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: Flipping a coin

    Write out how you would do this "by hand" without worrying about code at all. Pretend you're giving directions to somebody who has no idea how to count the highest number of coin flips. Be as specific and exact as possible. When you have that written out, it shouldn't be too hard to convert to code.

  3. #3
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Flipping a coin

    In a bunch of your code, your actual code is inside //. Fix that might help out

Similar Threads

  1. Question about my coin toss program
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 9th, 2010, 03:14 AM