這說明如下:試圖寫在對一個簡單的倒數循環用Ruby
數字Cruncher
收件採用一個數作爲參數 的方法如果該數量是大於20 計數從數字向下通過2的 如果號碼是從數小於20 計數減少1的 顯示的數字,因爲它們向下計數到0。
我已經寫了這個,但它沒有做它應該做的。任何幫助?
def num_cruncher(num)
count = num
until count == 0 do
if num > 20
puts count - 2
else
puts "#{count}"
end
count -= 1
end
end
你總是隻在這裏倒數1。 'count - 2'不會改變'count'。無論'count'的值如何,每循環執行一次'count - = 1'。你的邏輯應該(1)檢查'count'的值並根據你想要的規則遞減它,然後(2)打印'count'的當前值。 – lurker
當num爲20時會發生什麼? –