Semester Final: Coin Flip

Code

    /// Name: Kelsey Lieberman
    /// Period 5
    /// Program Name: CoinFlip
    /// File Name: CoinFlip.java
    /// Date Finished: 1/20/2016
    
import java.util.Scanner;
import java.util.Random;

public class CoinFlip
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        Random rng = new Random();
        
        int flips, numTails = 0, numHeads = 0, rerun = 1;
        ///"flips" is number entered by user to determine how many times to run the for loop. numTails and numHeads will be the number of times the coin landed on heads or tails.
        /// "rerun" is just to run the beginning of the program again if the user inputs an invalid number
        double probOfHeads, probOfTails;
        ///these doubles will be the probability of the coin landing on heads or tails based off the result of each flip recorded in the for loop.
        do{
        System.out.println("\nEyyyy lets flip a coin!");
        System.out.println("\nHow many times do you want to flip the coin?(1 - 2,147,483,647)");
        System.out.print("> ");
        flips = keyboard.nextInt();
            
        if (flips < 0 || flips > 2147483647)
            rerun = 0;
        /// gives rerun a value so the dowhile loop can be given a conditional
        }while (rerun == 0);
        ///do-while loop so program runs once before user enters a number for "flips" and more efficient than while loop in this case.
        
        for (int x = 1; x <= flips; x++)
        ///for loop because I need a part of the program to run a certain number of times, as entered by the user, and this is the most efficient way to accomplish that, that I know of.
        {
            int z = 1 + rng.nextInt(2);
            /// will give me a "random" number between 1 and 2. Inside the for loop because it has to be randomly generated for each iteration.
            String result;
            /// will change depending on whether z is 1 or 2, this will be the result of the coin flip.
            if (z == 1)
            {
               result = "Heads";
               numHeads++;
            }
            else
            {
                result = "Tails";
                numTails++;
            }
            ///^^ these assign "result" a value of "Heads" or "Tails" depending on whether z is 1 or 2.
            
            System.out.println("Flip #" + x + ": " + result + ".");
        }
        
        System.out.println("\nThe number of tails is " + numTails + ", and the number of heads is " + numHeads + ".");
        
        probOfHeads = ((double)numHeads / flips) * 100;
        probOfTails = ((double)numTails / flips) * 100;
        /// x100 because that will make it a percentage instead of a decimal.
        
        System.out.println("The probability of flipping heads is " + probOfHeads + "%, and the probability of flipping tails is " + probOfTails + "%.");
    }
}
/* An infinite number of flips are necessary to get as close to a consistent 50% chance of heads or tails being flipped as possible. The more flips,
the closer the probability of getting heads or tails is to 50%, consistently. Since an int only goes up to 2,147,483,647, then 2,147,483,646 (because it is divisible by 2)
is the number of flips necessary to get closest to a 50% of getting either heads or tails in this program. */
    

Picture of the output

Assignment S1F