Final #1
Code
/// Name: Kiran O'Farrell
/// Program Name: Semester 1 Final
/// File Name: Final1.java
/// Date Completed: 1/21/16
import java.util.Scanner;
import java.util.Random;
public class Final1 {
public static void main (String[] args) {
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
double flips_total;
int flips_made = 0;
int result;
double heads = 0;
double tails = 0;
double heads_percent;
double tails_percent;
System.out.println("How many times would you like to flip the coin?");
flips_total = keyboard.nextInt();
while (flips_total <= 0 || flips_total > 2147483647) {
System.out.println("Please select a valid number of flips.");
flips_total = keyboard.nextInt();
}
if (flips_total > 0 && flips_total <= 2147483647) {
while (flips_made < flips_total) {
result = r.nextInt(2);
if (result == 0) {
heads++;
flips_made++;
}
else {
tails++;
flips_made++;
}
if (flips_made == flips_total) {
heads_percent = (heads / flips_total) * 100;
tails_percent = (tails / flips_total) * 100;
System.out.println("There were " + heads + " heads and " + tails + " tails.");
System.out.println("You flipped heads " + heads_percent + "% of the time.");
System.out.println("You flipped tails " + tails_percent + "% of the time.");
}
}
}
}
}
Picture of the output