Final
Code
///Name: Stella Koliavas
///Period: 5
///File Name: Final
///Date Finshed: 1/20/16
import java.util.Random;
import java.util.Scanner;
public class Final
{
public static void main(String[] args)
{
int numberOfHeads = 0, numberOfTails = 0 ,flips = 0,tosses = 0, toss; /// all my variables are easy keywords
String coin;
double probOfHeads, probOfTails;
Scanner siri = new Scanner(System.in);
Random r = new Random();
System.out.print("How many times do you want to flip the coin? ");
flips= siri.nextInt();
while (flips != tosses) ///I used a while loop because it would continuously check the boundaries rather than using a do loop which checks after
{
toss = r.nextInt(2);
{
if (toss == 1 )
{
coin = "HEADS";
numberOfHeads++;
}
else
{
coin = "TAILS";
numberOfTails++;
}
System.out.println(coin);
tosses = tosses++;
tosses++;
}}
probOfHeads = (double)numberOfHeads/tosses;
probOfTails = (double)numberOfTails/tosses;
if (flips == tosses )
{
System.out.println("You flipped the coin " + (tosses) + " times!");
System.out.println("You rolled heads " + numberOfHeads +" times and tails " + numberOfTails+" times.");
System.out.println("The probability of getting heads is "+ (probOfHeads)+" and the probability of getting tails is " + (probOfTails));
}
}}
///the bigger number of flips you put in, the more likely your probability will be close to 50/50
Picture of the output