Question:
write a program that simulates flipping a coin repeatedly and continues unit three consecutive heads are tosed?
2012-10-29 17:50:24 UTC
at the end the program should display the total number of coin flips that were made.

// The "As7Ex3" class.
import java.awt.*;
import hsa.Console;

public class As7Ex3
{
static Console c; // The output console

public static void main (String[] args)
{

boolean one, two, three;
one = false;
two = false;
three = false;
int count = 0;
while (one == true && two == true && three == true)
{
one = true;
two = true;
three = true;
if (Math.random () < 0.5)
one = true;
if (Math.random () < 0.5)
two = true;
if (Math.random () < 0.5)
three = true;

count++;



}
c.println (count);

// Place your program here. 'c' is the output console
} // main method
} // As7Ex3 class


i keep getting wierd errors, plz help
Five answers:
nevie82
2012-10-29 18:15:10 UTC
your while loop will never work. the count will always be 0

it should be





while (!one || !two || !three)

{

one = false;

two = false;

three = false;

if (Math.random () < 0.5)

one = true;

if (Math.random () < 0.5)

two = true;

if (Math.random () < 0.5)

three = true;



count++;

}





@modulo

your attempt fails

the loop will stop after 3 consecutive head coin tosses but anywhere in time!

the meaning is to do 3 consecutive coin tosses and if only 1 fails you have to start again with the next 3 tosses.





edit

your console definition is wrong



instead c.println (count); (at As7Ex3.main(As7Ex3.java:34) this is line 34)

try System.out.println ("Number of coin toss attempts was " + count);
modulo_function
2012-10-29 18:17:30 UTC
** edited, took out incorrect garbage and provided a working, tested code...**



Use an array to store just the last three rolls. Use modulo arithmetic to access the array properly.

This code works. It took 18 rolls to get 3 consequtive heads:



int index = 0;

int[] lastThree = new int[3];

Arrays.fill(lastThree, 0);

Random rand = new Random();

int sum = 0;

int rolls = 0;

while (sum < 3) {

index = rolls % 3;

lastThree[index] = rand.nextInt(2);

rolls++;

sum = 0;

for (int k = 0; k < 3; k++) {

sum += lastThree[k];

}

System.out.printf(

"rolls: %d , 1,2,3: %d, %d, %d\n",

rolls, lastThree[0], lastThree[1], lastThree[2]);

}



results:

run, rolls

1, 18

2, 8

3, 3 // That has a probaility of 1/8 of happening

4, 6

5,5

6,7

...



+add

You'll need to import the Random and Arrays classes.
palacio
2016-12-24 21:46:35 UTC
each and every turn of the coin is an autonomous trial. the consequences of the previous flips has no influence on the possibility of a head on the subsequent turn. hence the possibility is the same on each and every turn for a head, thus, while you evaluate that's a honest coin, the possibility of a head on anybody turn is a million/2.
Tasm
2012-10-29 17:54:22 UTC
Define wierd errors
2014-07-05 23:45:38 UTC
sophisticated thing look into on to a search engine this can assist


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...