I am pretty new to java and using codehs, i need to find the amount and percentage of heads/ tails in a 100 toss program. I've got the 100 flips down but the percentage and printing the amount are beyond me, here is my code and thanks for the help
public class CoinFlips extends ConsoleProgram
{
public void run()
{
for (int i = 0; i < 100; i++)
{
if (Randomizer.nextBoolean())
{
System.out.println("Heads");
}
else
{
System.out.println("Tails");
}
}
}
}
You will need a counter and a variable for the result.
int TOTAL =100;
int counter =0;
for (int i = 0; i < TOTAL; i++) {
if (Randomizer.nextBoolean()) {
System.out.println("Heads");
counter++;
}else{
System.out.println("Tails");
}
double procent = (double)counter/TOTAL*100;
System.out.println("From "+ TOTAL +" flipped coins " + procent+"% were Heads" );
}