0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

条件分岐を使ったプログラム

Posted at

ペッパー君に20時から翌朝6時まで喋ってほしくないので、その時間中は「NG」、それ以外は「OK」と出力するメソットを作成します。
ペッパー君が喋る時をtrue,喋らない時をfalseとして、時刻も同時に入力します。

出力例は、

pepper_talk(true, 5)
=>NG
pepper_talk(true, 6)
=>OK
pepper_talk(false, 5)
=>OK
pepper_talk(false, 5)
=>OK

このようなイメージです。

まず、メソッドを作ります。

def pepper_talk(talking, hour)

end

出力例になるように処理を書いていきます。
喋ってはいけない時間を0時から6時までと、20時以降で分けて考えてあげると良いと思います。

def pepper_talk(talking, hour)
  if talking && (hour < 6 || hour >= 20 )
    puts "NG"
  else
    puts "OK"
  end
end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?