First Final
Code
//Tyler Hart
//period 5
//DisplayProbability.java
//Display Probability
//11/10/2015
import java.util.Random;
import java.util.Scanner;
public class DisplayProbability
{
public static void main(String[] args)
{
Scanner key = new Scanner(System.in);
Random r = new Random();
int d1;
int numberOfHeads=0,numberOftails=0, flips;//needed for math at end
System.out.println("how many flips?");
flips = key.nextInt();
int rflips= flips;
if (flips < 1 ||flips>2100000000)//easly makes sure the number is in usable bounds
System.out.println("error please restart");
else
{
while (rflips>0)
{
d1 = r.nextInt(2);
if (d1==1)
{
numberOfHeads++;
}
else
{
numberOftails++;
}
rflips= rflips-1;
}
System.out.println("you got "+numberOfHeads+" heads and "+numberOftails+" tails");
double probOfHeads = (double)numberOfHeads / flips;
System.out.println("Your probability of heads is " +probOfHeads);
double probOftails = (double)numberOftails / flips;
System.out.println("Your probability of tails is " +probOftails);
}
/*2,100,000,000 is consistently get as close to 50% as possible
but 100,000 is processed much faster and is always rounded to 50%
so 100,000 would be more effecent to use if you where to round*/
}
}
Picture of the output