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 1 year has passed since last update.

Ruby if,else問題

Posted at

if,elseを使ったプログラムを作成

2桁の正の整数を入力します。
その整数が、10の倍数(10,20,30...)からの差が2以内であるときはTrue、
それ以外はFalseと出力するメソッドを作りましょう。

模範解答
def near_ten(num)
  quotient = num % 10
  if quotient <= 2 || quotient >= 8
    puts "True"
  else
    puts "False"
  end
end

# 呼び出し例
near_ten(12)
ターミナル
True

解説

2桁の整数を10で割ったときの余り = quotient
if文では、その余り(quotient)が、2以下か8以上のとき"True"か"False"を出力するよう記述しています。

今回は2桁の数字が12です。
12 % 10 = 2
2以下であるため、Trueが返されました。

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?