如何编写一个包含 "good morning" 和 "good night" 的程序

How to write a program which says "good morning" and "good night"

我需要编写一个程序,根据 Ruby.[=13 中的时钟显示 "good morning"、"good afternoon"、"good evening" 和 "good night" =]

我尝试了一个不起作用的程序。

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

一种方法:

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)

您可以使用 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