【发布时间】:2017-08-12 10:09:02
【问题描述】:
我有一种方法可以在墙上唱出“啤酒瓶”。我有一个问题,当它达到“1”时,我希望瓶子是单数而不是复数。
我设法通过创建一个“if”来做到这一点。当@bottles 达到1 这有效。但是,当 until 语句达到 1 时,它会说
'Take one down, pass one round
one bottles of beer'
此时我需要 until 语句来跳过它。这是怎么做到的?
'
class BeerSong
require 'humanize'
attr_accessor :bottles
def initialize(bottles)
@bottles = bottles
@bottles = 0 if @bottles < 0
@bottles = 99 if @bottles > 99
end
public
def print_song
until @bottles == 0 do
break if @bottles == 0
puts "#{@bottles.humanize} bottles of beer on the wall"
puts "#{@bottles.humanize} bottles of beer"
puts "Take one down, pass one round"
puts "#{(@bottles - 1).humanize} bottles of beer"
@bottles = @bottles - 1
if @bottles == 1
puts "#{@bottle} bottle of beer on the wall"
puts "#{@bottles.humanize} bottle of beer"
puts "Take one down, pass one round"
puts "#{(@bottles - 1).humanize} bottles of beer"
@bottles = @bottles - 1
end
end
end
end
beer = BeerSong.new(99)
beer.print_song
【问题讨论】:
标签: ruby