我需要写一个程序,根据Ruby中的时钟,说“早上好”,“下午好”,“晚上好”和“晚安”。
我尝试了一个不起作用的程序。
def greet(hour_of_clock)
end
if hour == 6 -11 am
puts 'good morning'
elsif hour == 12-4 pm
puts 'good afternoon'
elsif hour == 5-8 pm
puts 'good evening'
else
puts 'good night'
end
local_time = time.new
hour_of_clock = local_time_hou
答案 0 :(得分:3)
您可以使用case
声明:
def greet(little_hand)
case little_hand
when (6..11) then puts "Good Morning"
when (12..16) then puts "Good Afternoon"
when (17..20) then puts "Good Evening"
else puts "Good Night"
end
end
答案 1 :(得分:2)
一种方法:
def greet(hour_of_clock)
if hour_of_clock >= 6 && hour_of_clock <= 11
puts "Good Morning"
elsif hour_of_clock >= 12 && hour_of_clock <= 16
puts "Good Afternoon"
elsif hour_of_clock >= 17 && hour_of_clock <= 20
puts "Good Evening"
else
puts "Good Night"
end
end
greet(Time.new.hour)