私はChris Pine Leap Year Exampleをやっています。問題は、ある範囲のうるう年をすべて教えてくれるメソッドを書くことです。ユーザーに開始年と終了年を尋ねます。
year
提供された回答を見ると、ステートメントでループされている変数がどのようにwhile
ステートメントに追加されているかについて混乱していますputs "Check it out...these years are leap years:"
。while
ループが分かります。毎年または結果がユーザーにどのように表示されているかわかりません。ステートメント内の変数とは別のスコープ内のステートメント内のyear
変数ではありませんか?while
year
puts
def leap_years
puts "Starting Year?"
start = gets.chomp.to_i
puts "Ending Year?"
ending = gets.chomp .to_i
puts "Check it out...these years are leap years:"
year = start #year is now = to the start, but how is it getting fed each year from the while loop?
while year <= ending
if year%4 == 0
if year%100 != 0 || year%400 == 0
puts year
end
end
year = year + 1
end
end