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.

最大値を求める関数

Last updated at Posted at 2021-11-20

##概要
最大値を求める関数をつくってみた

##手順
1.まずは配列でいくつかの数値を取得

arr = [3, 4, 5, 6, 7]

2.メソッドの中身

def saidai(arr)
  a = arr[0]
  arr.each do |i|
    if a < i
      a = i
    end
  end
    return a
end

####解説
・「a」という変数にarr[0]を定義する。
・先ほど取得した配列を1つずつチェックしていく。
・一つずつの要素(i)がaよりも大きかったら
 新しくa上書きしていく。

return a

で返り値は最後に上書きされたaの値になる。

0
0
2

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?