Semester One Final
Code
///Name: Luke Shin
///Period: 7
///Project Name: Semester One Final
///File Name: BabyCalculator.java
///Date: 01/21/2015
import java.util.Scanner;
import java.util.Random;
import java.util.InputMismatchException;
public class DisplayingProbability
{
public static void main( String[] args )
{
System.out.println(" Let's play some coin flips!!!\n");
int flips = getFlips();
int tails = 0;
int heads = 0;
Random r = new Random();
for (int i = 0; i < flips; i++)
{
if (r.nextInt(2)==0) tails++;
else heads++;
}
float headsProb = ((float) heads / flips * 100);
float tailsProb = ((float)tails/flips*100);
System.out.println("\n Heads: "+heads+" Tails: "+tails);
System.out.println("\n Heads probability: "+headsProb+"% Tails probability: "+tailsProb+"%\n");
System.out.println("\n See you next time!!!");
}
static int getFlips()
{
Scanner keyboard = new Scanner(System.in);
int flips = 0;
boolean exception = false;
while (flips<=0 || flips >= 2100000000)
{
keyboard = new Scanner(System.in);
exception = false;
System.out.print("\n Please input desired integer value of coin flips (0, 2.1 billion)\n >");
flips = keyboard.nextInt();
if ((flips<=0 || flips >= 2100000000)&&!exception)
System.out.println("\n Wrong Input!!! Has to be more than 0, but less than 2.1 billion!");
}
System.out.println();
return flips;
}
}
Picture of the output