I have some problems with a coin toss program

Dallas Langford picture Dallas Langford · Feb 22, 2016 · Viewed 7.3k times · Source

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");
        }
        }
    }
}

Answer

ΦXocę 웃 Пepe&#250;pa ツ picture ΦXocę 웃 Пepeúpa ツ · Feb 22, 2016

Problem:

You will need a counter and a variable for the result.

Solution

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" );
}