Assignment Final and Display Probability
Code
///Name: Trent Lane
///Period: 6
///Program Name: Final
///Date Finished: 1/22/16
import java.util.Random;
import java.util.Scanner;
public class DisplayProbability
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random rng = new Random();
int heads = 0;
int tails = 0;
int n = 0;
System.out.println("");
System.out.println("");
System.out.println("Welcome to the coin flipping simulator");
System.out.println("How many times would you like to flip the coin");
System.out.println("");
int flips = keyboard.nextInt();
do
{
int flip = rng.nextInt(2);
if ( flip == 1 )
heads++;
else
tails++;
n++;
}
while (n != flips);
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("Heads: " + heads + " ");
System.out.println("Tails: " + tails + " ");
System.out.println("");
System.out.println("");
double probOfHeads = (double)heads / flips;
double probOfTails = (double)tails / flips;
System.out.println("");
System.out.println("Probability of heads: " +probOfHeads + "%");
System.out.println("Probability of tails: " +probOfTails + "%");
System.out.println("");
System.out.println("");
}
}
//The number that can get you closest to 50/50 consistently is 1,000,000
Picture of the output