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 引数を使ってプログラムを作成する

Posted at

引数を使ってプログラムを作ろう

< 問題 >
ユーザーが数字を2つ渡すと、それらを掛け算した結果を返すプログラムを作ってください。
2つの数字を、それぞれnum1, num2という変数にgetsメソッドを利用して定義してください。

# 自分の回答
def two_nums(num1, num2)
  result = num1 * num2
  puts "#{num1}#{num2}をかけた答えは#{result}です"
end


puts "最初の数字を入力してください"
num1 = gets.to_i
puts "2番目の数字を入力してください"
num2 = gets.to_i

two_nums(num1, num2)
# 模範回答
def multiplication(num1, num2)
  puts "#{num1}#{num2}をかけた答えは#{num1 * num2}です!"
end

puts "最初の数字を入力してください"

num1 = gets.to_i

puts "2番目の数字を入力してください"

num2 = gets.to_i

multiplication(num1, num2)

解説

メソッドには複数の引数を渡すことができます。
複数の引数を渡した場合、引数を受け取る方は、メソッドの呼び出し順で引数を順番に代入されます。

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?