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.

10の倍数の差が2以内で条件分岐する方法

Last updated at Posted at 2023-02-14

復習しアウトプットします。

正の整数を入力し、その整数が、10の倍数(10,20,30...)からの差が2以内であるときはTrueそれ以外はFalseを出力するメソッドを作成
>出力例:
near_ten(12)True
near_ten(17)False
near_ten(19)True
自身の回答
def near_ten(num)
 if  num_x = num % 10
     num_x <= 2 || num_x >= 8
     puts "True"
 else
    puts "False"
 end
end
模範解答
def near_ten(num)
  quotient = num % 10
  if quotient <= 2 || quotient >= 8
    puts "True"
  else
    puts "False"
  end
end

惜しい。。。。惜しいい!!!!!
悔しい。笑

アプトプット解説

def near_ten(num)   
+ num_x = num % 10     #入力された数値を10で割り1の位の余りを算出
- if  num_x = num % 10
-     num_x <= 2 || num_x >= 8
+ if  num_x <= 2 || num_x >= 8   #一の位が0,1,2,8,9であれば差が2以内となりTrueとなる
     puts "True"
 else
    puts "False"
 end
end

ポイント
演算子について、違いを把握しておく必要がある。
a / b →除算
a % b →a を b で割った"余り"

まだまだ詰めが甘いな。。。
引き続き復習して行きます。

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?