Assignemnt # Final Exam
Code
/// Name: JJ Deng
/// Period: 6
/// Program Name: Final Exam
/// File Name: Final Exam One.java
/// Date Finished: 1/22/2016
import java.util.Scanner;
import java.util.Random;
import java.util.InputMismatchException;
public class FinalExamOne
{
public static void main( String[] args )
{
Random r = new Random();
int tails = 0; int heads = 0; int flips = getFlips();
for (int i = 0; i< flips; i++)
{
if (r.nextInt(2)==0)
tails++;
else
heads++;
}
double probHeads = ((double)heads / flips *100);
double probTails = ((double)tails / flips *100);
System.out.println( "Total Number of Heads ... " + heads);
System.out.println();
System.out.println( "Total Number of tails ... " + tails);
System.out.println();
System.out.println( "The probablity of rolling heads is... " + probHeads + "%");
System.out.println();
System.out.println( "The probablity of rolling tails is... " + probTails + "%");
System.out.println();
}
static int getFlips()
{
Scanner keyboard = new Scanner(System.in);
boolean exception = false;
int flips = 0;
while (flips <= 0 || flips >= 2100000000)
{
keyboard = new Scanner(System.in);
exception= false;
try
{
System.out.println( "Number of times you want to flip the coin " );
System.out.println();
flips = keyboard.nextInt();
}
catch (InputMismatchException e)
{
System.out.println( "HEY SUCKA THATS NOT ACCEPTABLE.");
flips = 0;
exception = true;
}
if (( flips <=0 || flips >= 2100000000) && !exception)
System.out.println( "PUT IN A NUMBER DATZ GREADA THAN ZERO AND LESS THAN 2100000000.");
System.out.println();
}
return flips;
}
}
Picture of the output