Semester 1 Final
Code
/// Name: DeeJay Cerezo
/// Period: 7
/// Program Name: Semester 1 Final
/// File Name: Final1.java
/// Date Finished: 1/21/2016
import java.util.Scanner;
import java.util.Random;
public class Final1
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random rng = new Random(); //scanner and a random number generator
int flips, heads, tails, variable, requestedflips;
flips = 0;
heads = 0;
tails = 0;
System.out.println(" How many times shall I flip this coin?");
requestedflips = keyboard.nextInt();
//In this case, doing a do-while loops is simpler for me than just a normal while loop
do {
int flip = rng.nextInt(2);
if (flip ==1)
{ heads++;
flips++; }
else
{ tails++;
flips++; }
} while (flips != requestedflips); //whenever the random picks between 1 or 2, it will add a number to whichever it picks. Each time, it adds a number to the number of times it has flipped. This continues until the number is equal to the number of flips the human has requested.
System.out.println("heads: " + heads + " tails: " + tails +".");
int numberOfHeads, numberOfTails, chances;
numberOfHeads = 50;
chances = 100;
double probOfHeads = (double)numberOfHeads / chances * 100;
numberOfTails = 50;
double probOfTails = (double)numberOfTails / chances * 100;
System.out.println ("The probability of getting heads: " + probOfHeads + "% and tails is " + probOfTails + "%.");
}
}
Picture of the output