Writing FizzBuzz

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

    You want probably make it configurable, but the question is what should be made configurable - we don't know that. Maybe we should make configurable all the cycle (FizzBuzz has the cycle). Here is very small and fun version with configurable cycle:

    string[] fizzBuzzCycle = 
        "FizzBuzz,{0},{0},Fizz,{0},Buzz,Fizz,{0},{0},Fizz,Buzz,{0},Fizz,{0},{0}"
        .Split(',');
    
    for (int i = 1; i <= 100; i++)
        Console.WriteLine(fizzBuzzCycle[i%fizzBuzzCycle.Length], i);
    

    So if the strings or whole cycle should be changed it is easy to change. But you just don't know what to make configurable. Maybe condition will change: "for prime numbers print Pizz" and for this modification the solution by @ThomasLevesque is better, because it is easier to change.

提交回复
热议问题