Writing FizzBuzz

后端 未结 30 2151
庸人自扰
庸人自扰 2020-12-04 08:50

Reading the coding horror, I just came across the FizzBuzz another time.

The original post is here: Coding Horror: Why Can\'t Programmers.. Program?

For thos

30条回答
  •  囚心锁ツ
    2020-12-04 09:10

    I think your implementation is unnecessarily complex. This one does the job and is easier to understand:

    public void DoFizzBuzz()
    {
        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);
        }
    }
    

提交回复
热议问题