Writing FizzBuzz

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

    I think you started with a complicated way. Improving that code would be more complicated. You can use a temp variable to track and display that variable at the end of the FizzBuzz check. Below is code and you can also watch this detail c# FizzBuzz youtube video ( http://www.youtube.com/watch?v=OX5TM3q-JQg ) which explains how the below code is implemented.

        for (int j = 1; j <= 100; j++)
        {
        string Output = "";
    
        if (j % 3 == 0) Output = "Fizz";// Divisible by 3 --> Fizz
    
        if (j % 5 == 0) Output += "Buzz"; // Divisible by 5 --> Buzz
    
        if (Output == "") Output = j.ToString(); // If none then --> number
    
        Console.WriteLine(Output); // Finally print the complete output
        }
    

提交回复
热议问题