Finals First Semester (Just keep on flippin...)
Code
/// Name: Amir Salehi
/// Period: 7
/// Program Name:Display Probability
/// File Name:DisplayProbability.java
/// Date Finished: 1/21/2016
import java.util.Random;
import java.util.Scanner;
public class DisplayProbability
{
public static void main ( String[] args )
{
Random r = new Random();
Scanner input = new Scanner(System.in);
int x = 1 + r.nextInt(2), flips;
int counter = 0;
double head = 0;
double tails = 0;
System.out.println(" Welcome to coin flipper!");
System.out.println("How many times would you like to flip? ");
flips = input.nextInt();
while ( flips < 0 && flips < 2100000000)//I use this while statement to not surpass the bounderies.
{
System.out.println(" Sorry, You have chosen an Invalid number.");
System.out.println("Please choose a number between 1 and 2100000000");
flips = input.nextInt();
}
while (flips != counter) //To count how many times it will flip before stopping.
{
x = 1 + r.nextInt(2);
if ( x == 1) {
System.out.print(" Heads ");
head++;
}if (x == 2) {
System.out.print(" Tails ");
tails++;
}
counter++;
}
System.out.println("");
System.out.println("Looks like you had " + head + " amount of Heads and " + tails + " amount of Tails!");
double probhead = head / flips;
double probtails = tails / flips;
System.out.println("Probablities of Heads = " + probhead);
System.out.println("Probablities of Tails = " + probtails);
}
}
Picture(s) of the output