我正在创建一个应用程序,它告诉从 1 到 12 的每个数字代表一年中的哪个月份,例如1 = January
,2 = February
等。这是初始代码
print "Please, tell me the month's number!"
number = gets.chomp.to_i
while number > 12 do
print "Please, re-type it again!"
number = gets.chomp.to_i
end
case number
when 1 then print "This is January!"
when 2 then print "This is February!"
when 3 then print "This is March!"
when 4 then print "This is April!"
when 5 then print "This is May!"
when 6 then print "This is June!"
when 7 then print "This is July!"
when 8 then print "This is August!"
when 9 then print "This is September!"
when 10 then print "This is October!"
when 11 then print "This is November!"
when 12 then print "This is December!"
else print "I can't undestand you, i'm sorry!"
end
现在,基本逻辑已经设定。我认为唯一缺少的是 while 循环中定义的第二个条件,如果输入不是整数,我需要重新输入它。我正在尝试正确定义该 while 循环中的第二个条件,但到目前为止没有结果。我该怎么做?
我怎样才能使代码更好?在这种情况下,while
循环是适合这项工作的正确方法吗?