Guest User

Untitled

a guest
Jun 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package lukelafrenieremath1350lab04;
  6.  
  7. /**
  8.  *
  9.  * @author Shifteh
  10.  */
  11. import java.util.Random;
  12.  
  13. public class LukeLafreniereMATH1350Lab04 {
  14.  
  15.     /**
  16.      * @param args the command line arguments
  17.      */
  18.     public static void main(String[] args) {
  19.         // TODO code application logic here
  20.  
  21.         int rounds = 1000;
  22.         float playerOneWins = 0;
  23.         float playerTwoWins = 0;
  24.         float playerOneWinRate = 0;
  25.         float coinsFlipped = 0;
  26.         float flipResult = 0;
  27.         int playerOneHeads = 0;
  28.         int playerTwoHeads = 0;
  29.  
  30.         for (int i = 0; i < rounds; i++) {
  31.             playerOneHeads = 0;
  32.             playerTwoHeads = 0;
  33.             coinsFlipped = 0;
  34.             while (playerOneHeads < 3 || playerTwoHeads < 3) {
  35.                 Random flipRandom = new Random();
  36.                 flipResult = flipRandom.nextInt(2) + 1;
  37.                 coinsFlipped++;
  38.  
  39.                 if (flipResult == 1) {
  40.                     if (coinsFlipped % 2 == 1) {
  41.                         playerOneHeads++;
  42.                     } else if (coinsFlipped % 2 == 0) {
  43.                         playerTwoHeads++;
  44.                     }
  45.                 }
  46.  
  47.                 if (playerOneHeads == 3) {
  48.                     playerOneWins++;
  49.                 } else if (playerTwoHeads == 3) {
  50.                     playerTwoWins++;
  51.                 }
  52.  
  53.             }//End of while loop
  54.  
  55.         }//End of for loop\
  56.         System.out.println(playerOneWins);
  57.         System.out.println(playerTwoWins);
  58.         playerOneWinRate = (playerOneWins / rounds);
  59.         System.out.println("after " + rounds + " rounds player 1 would win "
  60.                 + playerOneWinRate + "% of the time");
  61.  
  62.     }//End of main
  63. }//End of class
Add Comment
Please, Sign In to add comment