2014-10-10 61 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