Final Sem 1
Code
///Name: Ben Borglin
///Period: 6
///Program: Final Exam Sem 1
///File Name: Final1.java
///Date Finished: 1/22/16
import java.util.Scanner;
import java.util.Random;
public class Final1
{
public static void main( String[] args)
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int heads, tails, flips; //Used these variables to make this clear and simple
tails = 0;
heads = 0;
System.out.print("How many coin flips would you like to do: ");
flips = keyboard.nextInt();
while ( flips < 0 || flips > 2100000000 ) //I used a while loop because It was easier to type and it is more clear
{
if ( flips < 0 )
{
System.out.println("Too small of a number pick again.");
flips = keyboard.nextInt();
}
else
{
System.out.println("Too big of a number pick again.");
flips = keyboard.nextInt();
}
}
for (int n = 0; n < flips; n++) //I used a for loop to lessen the type load and all the variables are clearly labeled on top
{
int x = r.nextInt(2);
if ( x == 1 )
heads++;
else
tails++;
}
double probOfHeads = ((double)heads / flips)*100;
double probOfTails = ((double)tails / flips)*100;
System.out.println(" ");
System.out.println("Total number of heads: " + heads + "");
System.out.println("Probability of heads : " + probOfHeads + "%");
System.out.println(" ");
System.out.println("Total number of tails: " + tails + ".");
System.out.println("Probability of tails : " + probOfTails + "%");
}
}
///I notice that to get an exact 50/50 chance you must use 2.1 billion input number of coin flips
Picture of the output