使用三元条件运算符的 FizzBuzz
我一直在阅读 ruby 中的条件式表达式。 然而,我遇到了一个我不太理解的问题来定义经典的 FizzBuzz 问题。 我了解 FizzBuzz 问题,甚至在使用三元运算符找到以下快速解决方案之前编写了自己的问题。 如果有人可以向我解释这个链如何解决 FizzBuzz 问题,我将非常感激:)
for i in 0...100
puts i%3==0 ? i%5==0 ? "FizzBuzz" : "Buzz" : i%5==0 ? "Fizz" : i
end
I've been reading up on conditional-style expressions in ruby. However I came across one I couldn't quite understand to define the classic FizzBuzz problem. I understand the FizzBuzz problem and even wrote my own before finding the following quick solution utilising the ternary operator. If someone can explain to me how this chain works to satisfy the FizzBuzz problem it would be very much appreciated :)
for i in 0...100
puts i%3==0 ? i%5==0 ? "FizzBuzz" : "Buzz" : i%5==0 ? "Fizz" : i
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
一些括号可能会有所帮助:
因此,如果 i 可以被 3 整除,那么它会检查 i 是否也可以被 5 整除。如果是,则打印“FizzBuzz”,否则仅打印“Buzz”。 如果 i 不能被 3 整除,则它再次检查能否被 5 整除,如果是,则打印“Fizz”,否则仅打印 i。
Some parentheses might help:
So, if i is divisible by 3, then it checks whether i is also divisible by 5. If so, it prints "FizzBuzz" otherwise just "Buzz". If i is not divisible by three, then it checks divisibility by 5 again and prints "Fizz" if so, otherwise just i.
以下是 Jeff Atwood 文章 中所述的 FizzBuzz 问题的描述。
三元运算符是 if-else 语句的简写。 一般格式是:
所以如果我写:
相当于下面的代码:
其中 cond 是
i % 2 == 0
,evaluate_if_cond_is_true 是1
,evaluate_if_cond_is_false 是0 。
三元运算符的好处是它们可以组合。 这意味着当任一条件计算为 true 或 false 时要执行的语句可以是另一个三元运算符。
让我们以更易读的方式放置整个条件:
按照上面解释的规则,将其映射到 if-else 语句很容易:
这不是有效的代码,但因为三元运算符的结果内联在结果表达式中,所以它被用作put 命令的输入。
Here is a description of the FizzBuzz problem as stated in this Jeff Atwood article.
A ternary operator is shorthand writing for an if-else statement. The general format is:
So if I write:
Is equivalent to the following code:
Where cond is
i % 2 == 0
, evaluate_if_cond_is_true is1
and evaluate_if_cond_is_false is0
.The nice thing about ternary operators is that they can be combined. This means that the statement to execute when either condition evaluates to true or false can be another ternary operator.
Let put the entire condition in a more readable fashion:
And mapping this to if-else statements is easy with the rules explained above:
This is not valid code but because the result of the ternary operator is inlined in the result expression it is used as input for the puts command.
为了好玩,这里有另一种方法:
还有另一种方法:
For fun, here's another way:
And another:
三元是一个基本的 if-then 结构。
上面相当于...
或者,使用一些括号...
The ternary is a basic if-then structure.
The above is equivalent to...
Or, using some parens...
流程是:
the flow is:
只是为了好玩。 如果你想用 C# 来做。 这是一个简单的方法。 它基本上从你的 for 循环开始,它将打印从 1 到 100 的数字。然后它会询问你的索引“i”是否可以被 3 整除,如果是 5,则打印到控制台“FizzBuzz”。 否则,如果您的索引“i”可被 3 整除(如果为 true),则打印到控制台“Fizz”。 否则,如果您的索引“i”可被 5 整除(如果为 true),则打印到控制台“Buzz”。 否则只需打印“i”,它是你的整数。 我添加了制表符以提高可读性。
Just for fun. If you wanted to do it in C#. Here's a simple way. It basically starts with your for loop that will print numbers from 1 to 100. It then asks if your index "i" is divisible by 3 and 5 if true then print to the console "FizzBuzz". Else if your index "i" is divisible by 3 if true then print to the console "Fizz". Else if your index "i" is divisible by 5 if true then print to the console "Buzz". Else just print out "i" which is your integer. I added tabbing for better readability.