Writing FizzBuzz

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

    I am a beginner, here is my attempt:

    public void DoFizzBuzz()
       {
           for (int i = 1; i < 101; i++)
           {
    
               if ((i % 3 == 0) && (i % 5 == 0))
               {
                   Console.WriteLine("{0} FizzBuzz", i);
               }
               else if (i % 3 == 0)
               {
                   Console.WriteLine("{0} Fizz", i);
               }
               else if (i % 5 == 0)
               {
                   Console.WriteLine("{0} Buzz", i);
               }
               else
               {
                   Console.WriteLine(i);
               }
    
           }
           Console.ReadLine();
       }
    

    Is there anything wrong with my approach? Mine seems a lot simpler than everyone's else approach so it must be wrong.

提交回复
热议问题