Final and Display Probability
Code
/// Name: Tim Chuang
/// Period: 7
/// Program Name: Display Probability
/// File Name: DisplayProbability.java
/// Date Finished: 1/21/2016
import java.util.Scanner;
import java.util.Random;
public class DisplayProbability
{
public static void main ( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random rng = new Random();
int flips, steps, heads, tails; //these are the variables//
steps = 0;
heads = 0;
tails = 0;
System.out.println("Choose a number of times between 1-2,100,000,000 to flip the coin!");
System.out.print("> ");
flips = keyboard.nextInt();
do
{
int flip = rng.nextInt(2);
if ( flip == 1 )
{
heads++;
steps++; // for the flips//
}
else
{
tails++;
steps++;
}
} while ( steps != flips );
System.out.println("Your heads: " + heads + " and your tails: " + tails + ".");
int Heads, Tails, chances;
Heads = 50;
chances = 100;
double probOfHeads = (double)Heads / chances;
Tails = 50;
double probOfTails = (double)Tails / chances;
System.out.println("Probability of heads: " + probOfHeads + " prob of tails: " + probOfTails + ".");
}
}
Picture of the output