josh187
josh187

Reputation: 65

coin toss simulator won't count heads and tails

I am trying to make a coin toss simulator program that will toss a coin a number of times based on what the number the user entered. It will display the coin toss number and heads or tails. It will then display how many heads and tails was flipped but It doesn't display the correct number of times, I am not sure how to fix this

any help would be appreciated

Here is what I got so far

Console.Write("\n\n");
Console.Write("------------------------------------------------");
Console.WriteLine("\nThis Program simulates tossing a coin mutliple times");
Console.Write("------------------------------------------------");
Console.Write("\n\n");

int Heads = 0, Tails = 0;
int compChoice = 0;

int attempts;                       
Random rnd = new Random();
Console.WriteLine("How many coin tosses?");
coinTossChoice = int.Parse(Console.ReadLine());
attempts = coinTossChoice;
compChoice = rnd.Next(0, 2);

do
{


    if (attempts == coinTossChoice)
    {
        if (compChoice == 0)
        {
            Console.WriteLine("Toss Number# ");
            Console.WriteLine("\nHeads");
            Heads++;
        }
        else if (compChoice == 1)
        {
            Console.WriteLine("Toss Number# ");
            Console.WriteLine("\nTails");
            Tails++;
        }                                                                 
    }
} while (attempts != coinTossChoice);
Console.WriteLine("\nNumber of Heads {0} Number of Tails {1} .", Heads, Tails);

Upvotes: 1

Views: 332

Answers (1)

mdelapena
mdelapena

Reputation: 185

Here, there were some details in your code, I commented the changes I made so you can understand the idea.

        Console.Write("\n\n");
        Console.Write("------------------------------------------------");
        Console.WriteLine("\nThis Program simulates tossing a coin mutliple times");
        Console.Write("------------------------------------------------");
        Console.Write("\n\n");

        int Heads = 0, Tails = 0;
        int compChoice = 0;

        int attempts;
        Random rnd = new Random();
        Console.WriteLine("How many coin tosses?");
        int coinTossChoice = int.Parse(Console.ReadLine());
        //attempts is the counter for each toss
        attempts = 1;


        do
        {
            //compChoice is the coin toss containing 0 or 1 at random
            compChoice = rnd.Next(0, 2);
            if (compChoice == 0)
            {
                Console.WriteLine("Toss Number# " + attempts);
                Console.WriteLine("\nHeads");
                Heads++;
            }
            else if (compChoice == 1)
            {
                Console.WriteLine("Toss Number# " + attempts);
                Console.WriteLine("\nTails");
                Tails++;
            }
        //increment attempt
        attempts++;
        //cycle as many times as the user requested
        } while (attempts <= coinTossChoice);
        Console.WriteLine("\nNumber of Heads {0} Number of Tails {1} .", Heads, Tails);
        Console.ReadKey();

Upvotes: 1

Related Questions