1
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.

複数の整数で各々の差の条件で真偽判定

Last updated at Posted at 2020-10-08

【概要】

1.結論

2.どのようにプログラムするか

3.開発環境

1.結論

absメソッド、ifを使う!

2.どのようにプログラムするか

def close_far(a,b,c) #---❶
  x_mar = (a-b).abs
  y_mar = (a-c).abs
  z_mar = (b-c).abs
 if (x_mar == 2 && z_mar >= 3) || (y_mar == 2 && z_mar >= 3) #---❷
   puts "True"
  else
   puts "False"
 end
end

今回は整数を3つにしました。

❶:absメソッドを使用し、正負関係なしに絶対値を返しそれぞれを変数に代入しています。
❷:今回は「"a"と"b”の差が2 または "a"と"b”の差が2
かつ "b"と"c”の差が3」としています。下記のようにコーディングすることも可能ですが、上記のように「()と||(OR)と&&(AND)」で一つにまとめることも可能です。

  if x_mar == 2 && z_mar >= 3
    puts "True"
  elsif y_mar == 2 && z_mar >= 3
    puts "True"
  else
    puts "False"
  end

3.開発環境

Ruby 2.6.5
Rails 6.0.3.3
Visual Studio Code 1.49.2

1
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
1
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?