Simulate playing a game where you flip two coins if both coi

Simulate playing a game where you flip two coins. if both coins are the same value (heads, tail) you win. if have not win, flip 3 coins If these 3 coins have the same value. you win else you lose. play the game 100 times and output the winning percentage with a System pnrintin.

Solution

import java.util.Random;

public class CoinsGame {

   private static boolean CheckWinChance() {
       if (FlipTwoCoins()) {
           return true;
       }
       return FlipThreeCoins();
   }

   private static boolean FlipTwoCoins() {
       Random r = new Random();
       if (r.nextInt(2) == r.nextInt(2))
           return true;
       return false;
   }

   private static boolean FlipThreeCoins() {
       Random r = new Random();
       int a = r.nextInt(2), b = r.nextInt(2), c = r.nextInt(2);
       if (a == b && b == c)
           return true;
       return false;
   }

   public static void main(String[] args) {
       int winCount=0;
       for (int i = 0; i < 100; i++) {
           if (CheckWinChance()) {
               winCount++;
           }
       }
       System.out.println(winCount);
   }
}

 Simulate playing a game where you flip two coins. if both coins are the same value (heads, tail) you win. if have not win, flip 3 coins If these 3 coins have t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site