Search code examples
javacoin-flipping

I have some problems with a coin toss program


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

Solution

  • Here is a possible solution:

    Add:

    int headCount = 0;
    int tailsCount = 0;
    

    You can use them by:

            if (Randomizer.nextBoolean())
        {
            System.out.println("Heads");
            headsCount++;
        }
        else
        {
        System.out.println("Tails");
            tailsCount++;
        }
    

    Then write a method to calculate the percentage. Since this looks like a homework assignment, I'll leave that to you.