Writing FizzBuzz

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

    The null-coalescing operator is really useful:

    string output = null;
    for (int i = 1; i <= 100; i++)
    {
         if (i % 3 == 0) output += "fizz";
         if (i % 5 == 0) output += "buzz";
         Console.WriteLine(output ?? i.ToString());
         output = null;
    }
    Console.ReadKey();
    

提交回复
热议问题