logo CodeStepByStep logo

CoinFlip

Language/Type: C# parameters Random
Related Links:

Write a method named CoinFlip that simulates repeatedly flipping a two-sided coin until a particular side (Heads or Tails) comes up several times consecutively (in a row). Your method accepts two parameters, an integer k and a character side, where side is expected to be 'H' for Heads or 'T' for Tails. You should keep simulating the flipping of the coin until k occurrences of the given side are seen consecutively. For example, if the call is CoinFlip(3, 'H'); , you should flip the coin until Heads is seen 3 times in a row. Here is an example output from the call of CoinFlip(4, 'T');

T H T H T T H T T H H T H H H H H T T T T
You got T 4 times in a row!

If a negative k is passed, and/or a side value is passed that is not 'H' or 'T', your method should print ERROR! and exit immediately.

Use a Random object to give an equal chance to a head or a tail appearing. Each time the coin is flipped, what is seen is displayed (H for heads, T for tails), separated by spaces. When k consecutive occurrences of the given side occur, a congratulatory message is printed. Match our output format exactly.

Method: Write a C# method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.