Writing FizzBuzz

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

    public void DoFizzBuzz()
    {
        for (int i = 1; i <= 100; i++)
        {
            if (i % 3 == 0)
                Console.Write("Fizz");
            if (i % 5 == 0)
                Console.Write("Buzz");
            if (!(i % 3 == 0 || i % 5 == 0))
                Console.Write(i);
    
            Console.Write(Environment.NewLine);
        }
    }
    

    This gets rid of the bool found, but forces you to do duplicate evaluation. It is slightly different from some of the other answers using i % 15 == 0 for the FizzBuzz qualification. Whether or not this is better is up for debate. However, it is a different way.

提交回复
热议问题