我正在做 Chris Pine 闰年示例。问题是编写一个方法来告诉你一个年份范围内的所有闰年。您向用户询问开始年份和结束年份。
查看提供的答案,我对语句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