Writing FizzBuzz

后端 未结 30 2076
庸人自扰
庸人自扰 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:11

    I would suggest this compact code as an addition to the previous simple and nice versions.

    for (int i = 1; i <= 100; i++) // i++ but not ++i as in your example, be careful here
    {
        bool fizz = i % 3 == 0;
        bool buzz = i % 5 == 0;
        string output = fizz && buzz ? "FizzBuzz" :
                                fizz ? "Fizz" :
                                buzz ? "Buzz" :
                                i.ToString();
    
        Console.WriteLn(output);
    }
    

提交回复
热议问题