# coin toss program(Eclipse)

• 08-05-2010, 06:34 PM
ccie007
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:

tails
tail
tail

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()){

coinflip+=1;

}else{

coinflip-=1;

println("TAILS");

}

}
print(coinflip);
}
}
• 08-05-2010, 07:17 PM
Norm
Quote:

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.
• 08-05-2010, 11:46 PM
bs3ac
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?
• 08-06-2010, 09:36 AM
JosAH
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:

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

kind regards,

Jos
• 08-06-2010, 09:49 AM
j2me64
Code:

```while (count < 3) {     // do your stuff }```
• 08-06-2010, 07:03 PM
ccie007
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.