読み込んだ月の季節を表示 (switch文)

明解C言語 入門編 > 3. プログラムの流れと分岐 >

読み込んだ月の季節を表示 (switch文)

Ruby
print "何月ですか:"
month = STDIN.gets.chomp.to_i

case month
    when 3..5
        puts "春です。"
    when 6..8
        puts "夏です。"
    when 9..11
        puts "秋です。"
    when 1, 2, 12
        puts "冬です。"
    else
        puts "そんな月はありませんよ!!"
end

実行結果

L:\>ruby l:\lesson_03_023.rb
何月ですか:5
春です。

L:\>ruby l:\lesson_03_023.rb
何月ですか:8
夏です。

L:\>ruby l:\lesson_03_023.rb
何月ですか:13
そんな月はありませんよ!!