Results 1 to 6 of 6
  1. #1
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default coin toss program(Eclipse)

    I am trying to write a program which tosses coin and shows "heads" and "tails".It will continue to show heads and tails randomly untill it shows "heads" 3 times consecutively.

    Sample output should be like this:

    heads
    tails
    heads
    heads
    tail
    tail
    heads
    heads
    heads.

    I have almost written the program but can't figure out how stop with program when it shows "heads" 3 times consecutively.

    My code at the moment(mind you I am using Eclipse):

    import acm.program.*;
    import acm.util.*;
    import java.awt.*;



    public class chapter6ques2 extends ConsoleProgram{


    public void run(){


    RandomGenerator rgen = new RandomGenerator();

    int coinflip = rgen.nextBoolean() ? 0 : 0;


    for(int i=1;coinflip<=3;++i){



    if (rgen.nextBoolean()){


    println("HEADS");
    coinflip+=1;



    }else{

    coinflip-=1;

    println("TAILS");




    }


    }
    print(coinflip);
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    can't figure out how stop with program when it shows "heads" 3 times consecutively
    Do you have a design or algorithm to solve your problem?

    Can you post the steps you want to take in the program to solve the problem?
    Until you have a design for your code, its a waste of time to write code.

  3. #3
    bs3ac is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default

    How about storing past results to the last 3 values using a collection. That way when all three results equal heads or tails then the program can be told to stop?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default

    Suppose you haven't counted three consecutive 'head's yet and a 'tail' is thrown. No matter how many heads you counted before a tail should reset the number of consecutive heads to zero again. So the counting scheme runs as follows:

    if (head) count++
    else if (tail) count= 0
    if (count == 3) ... // three consecutive heads counted

    kind regards,

    Jos

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Java Code:
    while (count < 3) {
        // do your stuff
    }

  6. #6
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    Thank you guys.The problem is resolved.I am trying participate in this forum as little as possible because I want to push my brain.Only at last resort i come to seek for help here.I guess that would be idea to learn.cheers.

Similar Threads

  1. Java Coin Acceptor?
    By starzsimon in forum Advanced Java
    Replies: 5
    Last Post: 08-06-2011, 07:50 AM
  2. A tab in an eclipse RCP program
    By Drun in forum Eclipse
    Replies: 0
    Last Post: 05-20-2010, 03:56 PM
  3. Random coin flip application
    By Boomer1 in forum New To Java
    Replies: 8
    Last Post: 12-18-2009, 02:57 AM
  4. Replies: 2
    Last Post: 11-11-2007, 08:07 AM
  5. DB program in Eclipse
    By Unni in forum Eclipse
    Replies: 3
    Last Post: 08-09-2007, 11:07 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
  •