如何在Ruby中编写负循环,如for(i = index; i> = 0; i-) [英] How to write negative loop in ruby like for(i=index; i >= 0; i --)
本文介绍了如何在Ruby中编写负循环,如for(i = index; i> = 0; i-)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Ruby中编写一个倒计时的循环,类似于以下C样式的for
循环?
How can I write a loop in Ruby that counts down, similar to the following C-style for
loop?
for (i = 25; i >= 0; i--) {
print i;
}
推荐答案
在Ruby中执行递减循环的方法有很多:
There are many ways to perform a decrementing loop in Ruby:
第一种方式:
for i in (10).downto(0)
puts i
end
第二种方式:
(10).downto(0) do |i|
puts i
end
第三种方式:
i=10;
until i<0
puts i
i-=1
end
这篇关于如何在Ruby中编写负循环,如for(i = index; i> = 0; i-)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文