Wednesday 15 April 2015

java - Flipping three consecutive heads then program exits using RandomGenerator -


I'm stuck on a problem that I do not know how to solve. I use the random generator to simulate flipping a coin until RandomGenerator 3 flips continuously head when point at which point the program exits.

The variable flip quator tracks the total number of flip and the variable head flip flip continuously to the head. If the situation says that the flip head starts, the head starts to flip 3, but if the flip tail is the head of the next reset the flip is zero.

When I run the program, it only prints the last statement in my run method and whenever the loop does not run. I do not know why this program is not working because I hope he will work.

  import acm.program. *; Import acm.util. *; Public Class Head Flip-to-Innova Console Program {Public Zero Run (Expansion) {int flipCounter = 0; Int headFlip = 0; While (headsFlip == 3) {string flip = coinflip (); Println (flip); If (flip.equals ("heads")) {headsFlip + = 1; } And {headsFlip = 0; } Flip Counter ++; Flip = coinflip (); } / * While the end of the loop * / println ("took it" + flip counter + "flips to reach 3 heads continuously."); } / * End of Run Method * / Private String CoinFlip () {String Flip = Rajan. Nxblueen ()? "heads tails"; Return flip; } Private random generator rgen = RandomGenerator.getInstance (); }    

Note that a while while loop continues As long as the condition is true , it means that

  while (headFlip == 3)   

only until < Code> headsFlip executes the body of the loop 3 since the first time the loop has started, headflip is 0 , loop Not executed.

Html>

No comments:

Post a Comment