2014-10-10 34 views
0

我能够倒数到97瓶啤酒,但我无法循环倒数到1.是否可以用我写的东西创建循环?这是迄今为止我所拥有的。用99瓶啤酒循环麻烦的字符串

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!' 

回答

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 
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))百倍,number990

另请注意,将数字作为字符串((99).to_s)进行转换并且在需要时将其转换为整数效率很低(并且难以辨认);因此,相反,它总是一个整数,并且在你需要它作为一个字符串之前将它转换为字符串,当你显示时(或者让字符串连接/插值自动完成,就像这段代码一样)。

虽然红宝石确实有两个forwhile循环,他们很少使用(while)或从不(for)。相反,Rubyists通常依赖迭代器和枚举器。其他功能,如Integer#downtoInteger#upto,Integer#times和几乎所有的最真棒Enumerable mixin。

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 
0

事情你可以用它让你的生活更轻松:downto()proc,三元如果(凹口 - - 东西 - >? )

我很享受第一次体验这个练习,所以我对在这里提供答案有点犹豫,但事实如此。

它有点高级,但使用'proc'来确保正确地复制“瓶子”是一个很好而干净的方法来完成它。

'downto()'也是一个很好的方式来遍历这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 

来源:学会克里斯·派恩编程第2版(我改变了一些东西虽然)

+0

你是说像使用printf? [printf](http://ruby-doc.org/core-2.2.3/Kernel.html#method-i-printf) 您可以使用这些字段类型字符来格式化printf:[sprint](http:// ruby-doc.org/core-1.9.3/Kernel.html#method-i-sprintf) – 2015-10-04 03:35:50

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