我能够倒数到97瓶啤酒,但我无法循环计数到1.是否有可能用我写的东西创建一个循环?这是我到目前为止所拥有的。
all_beers = (99).to_s
one_less = ((all_beers).to_i - 1).to_s
puts '' +
all_beers + ' bottles of beer on the wall, ' +
all_beers + ' bottles of beer. You take one down you pass it around ' +
one_less + ', beers on the wall!'
all_beers = one_less
one_less = ((all_beers).to_i - 1).to_s
puts '' +
all_beers + ' bottles of beer on the wall, ' +
all_beers + ' bottles of beer. You take one down you pass it around ' +
one_less + ', beers on the wall!'
答案 0 :(得分:1)
使用downto:
它将从您想要的数字循环到您想要的数字。
99.downto(1).each do |s|
all_beers = s
one_less = s - 1
puts '' +
all_beers.to_s + ' bottles of beer on the wall, ' +
all_beers.to_s + ' bottles of beer. You take one down you pass it around ' +
one_less.to_s + ', beers on the wall!'
end
答案 1 :(得分:0)
是的,这当然是可能的。这取自99 Bottles of Beer project:
#
# Rubeer.rb
# by Eric Budd, Jan. 2008
#
# Demonstrates adding functionality to a built-in class, optional method parameters, inline
# conditionals, string replacement, alcohol aversion, and excessively fancy use of hashes.
#
# This borrows the hash from Daniel Straight's excellent implementation for the "wordalize" method
#
class Integer
NUMBER_WORDS = { 0 => "no", 1 => "one", 2 => "two", 3 => "three", 4 => "four", 5 => "five",
6 => "six", 7 => "seven", 8 => "eight", 9 => "nine",
10 => "ten", 11 => "eleven", 12 => "twelve", 13 => "thirteen",
14 => "fourteen", 15 => "fifteen", 16 => "sixteen", 17 => "seventeen",
18 => "eighteen", 19 => "nineteen",
20 => "twenty", 30 => "thirty", 40 => "forty", 50 => "fifty", 60 => "sixty",
70 => "seventy", 80 => "eighty", 90 => "ninety"}
def wordalize
raise "Invalid number to wordalize - should be in the range (0..99)" unless (0..99) === self
return NUMBER_WORDS[self] if self < 20
wordalized = NUMBER_WORDS[self - (self % 10)]
wordalized += '-' + NUMBER_WORDS[self % 10] unless (self % 10) == 0
return wordalized
end
def bottles
raise "Invalid number of bottles - should be in the range (0..99)" unless (0..99) === self
how_many_bottles = self.wordalize + ' bottle'
how_many_bottles += 's' unless self == 1
return how_many_bottles
end
alias :bottle :bottles # for grammar Nazis
end
def sing(number, teetotaller = false)
beverage = teetotaller ? 'coke' : 'beer'
puts "#{number.bottles.capitalize} of #{beverage} on the wall, #{number.bottles} of #{beverage}."
if number != 0
puts "Take one down, pass it around, #{(number - 1).bottles} of #{beverage} on the wall.\n\n"
else
puts "Go to the store and buy some more, 99 bottles of #{beverage} on the wall."
end
end
99.downto(0) { |number| sing(number) }
# Uncomment the following for the alternative teetotaller version
# 99.downto(0) { |number| sing(number, true) }
上传了多个Ruby版本,但默认版本对于初学者来说太复杂了。但是,这个非常好,你应该能够理解发生了什么。
应该回答你问题的一点是99.downto(0) { |number| ... }
。这是一个循环,它将重复大括号内的任何内容(在本例中为sing(number)
)一百次,number
从99
转到0
。
另请注意,将数字作为字符串((99).to_s
)携带并在需要时将其转换回整数是低效的(并且难以辨认);相反,让它始终是一个整数,并在你需要它作为字符串之前将其转换为字符串,当你显示时(或者让字符串连接/插值自动为你做,就像在这段代码中一样)。 p>
虽然Ruby确实同时具有for
和while
个循环,但它们很少使用(while
)或从不使用for
。相反,Rubyists通常依赖于迭代器和枚举器。 Integer#downto
等其他功能是Integer#upto
,Integer#times
以及最令人敬畏的Enumerable
混合中的所有内容。
答案 2 :(得分:0)
简短的回答是肯定的,你可以使用你编写的内容进行循环,并且有许多循环使用ruby的方法。然而,由于这似乎是关于学习编程,考虑到你没有使用任何控制结构或字符串插值,更不用说没有多大意义的演员,我建议Why's Poignant Guide to Ruby来在使用ruby时学习编程的概念。
除了downto之外,你还可以做点什么:
(1..99).reverse_each do |number|
bottle = number == 1 ? 'bottle' : 'bottles'
verse = "#{number} #{bottle} of beer on the wall, #{number} #{bottle} of beer. "
verse << "Take one down, pass it around, #{number-1} #{number-1 == 1 ? 'bottle' : 'bottles'} of beer on the wall"
puts verse
end
答案 3 :(得分:0)
您可以用来让生活更轻松的事情:downto(),proc,三元if( - that - thing - &gt;?:)
我很高兴第一次体验这项运动,所以我对在这里提供答案有点犹豫,但事实如此。
它有点先进,但使用了一个&#39; proc&#39;确保你复制瓶子和#34;正确是一个很好的,干净的方式来完成它。
&#39; DOWNTO()&#39;也是一种很棒的方式来迭代这99瓶,因为它让你觉得你在阅读英语而不是代码。
num_at_start = 99 # You may change this number.
num_bottles = proc { |n| "#{n} bottle#{ n == 1 ? '' : 's'}" }
num_at_start.downto(1) do |num|
print "#{num_bottles.call(num)} of beer on the wall, " +
"#{num_bottles.call(num)} of beer!\n" +
"You take one down, pass it around, "
unless num == 1
puts "#{num_bottles.call(num - 1)} of beer on the wall!"
else
puts "No more bottles of beer on the wall!"
end
end
来源:学习Chris Pine的第2版程序(我改变了一些事情)
答案 4 :(得分:0)
这是我在这个问题上找到的解决方法:
beer = 99
while beer > 0
puts beer.to_s + " bottles of beer on the wall. " + beer.to_s +
" bottles of beer."
(beer -= 1).to_s
puts "Take one down, pass it around. " + beer.to_s +
" bottles of beer on the wall."
end