0
0

More than 3 years have passed since last update.

Ruby 数値参照プログラム

Posted at

自分用です
簡単な数値参照プログラムを作りました。
下記コード

qiita.rb
m = gets.chomp
n = gets.to_i


room = []

n.times do
  count = gets.chomp
  room.push(count)
end

a = 0
count = n

while n > 0

  if ! (room[a]).include?(m)
    puts room[a].to_i
  else
    count -= 1
    if count == 0
      puts "none"
    end
  end
  n = n - 1
  a = a + 1
end

最初に変数mに参照元となる数値を入力。(文字データになってますけど)
それをもとに数値入力して当てはまらない数値だけ出力しています。

全てあてはまらなかった場合"none"と返します。

今回のif文は”あてはまらない”場合なので if !という形で作りました。
又、文字列の方がinclude?で簡単に参照できると思ったので
最初に文字列で参照し、出力時に数値に変換するフローで
作ってみました。

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