Assignment# Final Semester 1
Code
/// Name: Tommy Oyuntseren
/// Period: 7
/// Program Name: DisplayProb
/// File Name: DisplayProb.java
/// Date: 1/21/2016
import java.util.Scanner;
import java.util.Random;
public class DisplayProb
{
public static void main( String[] args )
{
System.out.println(" Welcome to Coin Flip probability program!\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 Thank you for using our products!");
}
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();
}
{
System.out.println("\n Wrong Input!!! Has to be an integer!");
exception = true;
flips = 0;
}
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