Oretas.L
Oretas.L

Reputation: 11

C# Simple Coin flip algorithm not working

I have a problem with my code, it should ask the user how many times they want to flip a coin and then proceed to flip that coin the stated number of times and then say how many heads and tails there were. The problem is that the program asks how many times the coin should be flipped and then just closes after user input. Can someone please tell me what I did wrong.

static void Main(string[] args)
{
    int heads = 0;
    int tails = 0;
    int counter = 0;
    Random coinflip = new Random();

    Console.WriteLine("How many times would you like to flip a coin? ");
    counter = Convert.ToInt32 (Console.ReadLine());

    for (int i = 0; i < counter; i++)
    {
        int flip = coinflip.Next(1, 3);
        if (flip == 1)
        {
           heads++;
        }
        else 
        {
           tails++;
        }
    }

    Console.WriteLine("You flipped a coin " + counter 
       + "times " + "and you got " + heads + "heads and " + tails + "tails.");
    Console.WriteLine();
}

Upvotes: 0

Views: 2756

Answers (1)

nolnah93
nolnah93

Reputation: 134

Try changing the final

Console.WriteLine()

to

Console.ReadKey()

That should keep the window open until you type a key, allowing you to see your output.

Upvotes: 5

Related Questions