Display Probability (FINAL)
Code
/// Name: Yordan Rashkov
/// 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 )
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int maximum, coin;
double p1, amount, heads, tails, p2;
heads = 0;
tails = 0;
amount = 0;
maximum = 0;
coin = r.nextInt(2);
// It took me probably half of the period to figure out that I need to do double(s) and Int(s). I originally thought that I could go with String(s) and Int(s) because I copied the "flip again" program from the past. However that didn't work out so well. (LoL)
do {
System.out.println(" How many flips would you like? : ");
maximum = keyboard.nextInt();
}while ( maximum > 2100000000 && maximum < 1 );
while (maximum < 2100000000 && maximum > 1 )
{
if ( coin == 1)
{
System.out.println(" Your flip turned out to be HEADS! ");
amount = amount + 1;
coin = r.nextInt(2);
maximum = maximum - 1;
heads = heads + 1;
amount = amount + 1;
}
else if ( coin == 0 )
{
System.out.println(" Your flip turned out to be TAILS! ");
amount = amount + 1;
coin = r.nextInt(2);
maximum = maximum - 1;
tails = tails + 1;
amount = amount + 1;
}
}
// I decided to use while loops due to the fact that I have used them for the majority of the time. Do-while is also a good way to go here, except I'd like to keep it basic... While loops are pretty easy to remember how to do.
p1 = heads / amount * 100;
p2 = tails / amount * 100;
System.out.println(" Your coin landed on heads " + heads + " time(s)... ");
System.out.println(" Your coin landed on tails " + tails + " time(s)... ");
System.out.println(" There is a " + p1 + " % chance that it landed on HEADS... ");
System.out.println(" There is a " + p2 + " % chance that it landed on TAILS... ");
}
}