repeated coin flip program in java code example
Example: repeated coin flip program in java
import java.io.*;
import java.math.BigInteger;
public class Main
{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
BigInteger counter=BigInteger.ZERO;
BigInteger num_of_flips=BigInteger.ZERO;
BigInteger num_heads=BigInteger.ZERO;
BigInteger num_tails=BigInteger.ZERO;
int flip;
boolean track_data=true;
System.out.println("Please input the number of coin flips to do.");
num_of_flips=new BigInteger(br.readLine());
while(counter.compareTo(num_of_flips)<0){
flip = CoinFlip();
System.out.print(flip);
if (track_data==true){
if (flip==1){
num_heads=num_heads.add(BigInteger.ONE);
}
else{
num_tails=num_tails.add(BigInteger.ONE);
}
}
counter=counter.add(BigInteger.ONE);
}
if (track_data==true){
System.out.println();
System.out.println("Heads appeared "+num_heads+" times.");
System.out.println("Tails appeared "+num_tails+" times.");
}
}
public static int CoinFlip(){
int i;
i = (int)(2*Math.random());
return i;
}
}