Results 1 to 6 of 6
Thread: coin toss program(Eclipse)
- 08-05-2010, 06:34 PM #1
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
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);
}
}
- 08-05-2010, 07:17 PM #2
Do you have a design or algorithm to solve your problem?can't figure out how stop with program when it shows "heads" 3 times consecutively
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 #3
Member
- Join Date
- Oct 2009
- Posts
- 10
- Rep Power
- 0
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 #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
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
- 08-06-2010, 09:49 AM #5
Java Code:while (count < 3) { // do your stuff }
- 08-06-2010, 07:03 PM #6
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
Similar Threads
-
Java Coin Acceptor?
By starzsimon in forum Advanced JavaReplies: 5Last Post: 08-06-2011, 07:50 AM -
A tab in an eclipse RCP program
By Drun in forum EclipseReplies: 0Last Post: 05-20-2010, 03:56 PM -
Random coin flip application
By Boomer1 in forum New To JavaReplies: 8Last Post: 12-18-2009, 02:57 AM -
Problem calling classes to flip coin x number of times and record heads or tails
By adlb1300 in forum New To JavaReplies: 2Last Post: 11-11-2007, 08:07 AM -
DB program in Eclipse
By Unni in forum EclipseReplies: 3Last Post: 08-09-2007, 11:07 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks