2

I have just made a simple game of fizz buzz( its where numbers go up and if divisible by 3 it is called fizz and if divisible by 5 its called buzz, and if divisible by both its called fizz buzz) and it works, however, I need to press enter to get the next number which i don't want to do. I want it that the numbers go up automatically. Can you help me please? this is my code

static void Main(string[] args)
{
    for (int i = 1; i <= 100; i++)           
    {
        bool fizz = i % 3 == 0;
        bool buzz = i % 5 == 0;
        if (fizz && buzz)
            Console.WriteLine("fizzbuzz");
        else if (fizz)
            Console.WriteLine("fizz");
        else if (buzz)
            Console.WriteLine("buzz");
        else
            Console.WriteLine(i);
        Console.ReadLine();
    }
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
James
  • 37
  • 1

1 Answers1

2

In the code you've posted Console.ReadLine() occurs on every iteration of your for-loop, so the program waits for input after every number. Move it outside of the loop to get your desired behavior, like this:

static void Main(string[] args)
{
    for (int i = 1; i <= 100; i++)
    {
        bool fizz = i % 3 == 0;
        bool buzz = i % 5 == 0;
        if (fizz && buzz)
            Console.WriteLine("fizzbuzz");
        else if (fizz)
            Console.WriteLine("fizz");
        else if (buzz)
            Console.WriteLine("buzz");
        else
            Console.WriteLine(i);
    }
    Console.ReadLine();
}

Best wishes on your new learning endeavor!

Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
Jon
  • 569
  • 2
  • 11