0

我目前正在从事一个小型学校项目(Ruby),但我收到了这个错误(参数数量错误(0 表示 2)),这让我想翻转一张桌子。:(

这是我试图开始工作的代码:

puts "How much does the product cost?"
price = gets.to_f.round

puts "How much money will you give for it?"
money = gets.to_f

change = calculate_change(price, money)

我用它来获取用户输入,将第一个四舍五入为固定数字,然后将第二个四舍五入为浮点数。这就是我的 calculate_change 方法的样子:

def calculate_change(price, money)
    return money - price
end
4

1 回答 1

2

根据@hirolau 的评论,您需要确保calculate_change()在调用它之前声明。

def calculate_change(price, money)
    money - price                    # return is optional in Ruby!
end

puts "How much does the product cost?"
price = gets.to_f.round

puts "How much money will you give for it?"
money = gets.to_f

change = calculate_change(price, money)
于 2013-09-12T05:04:42.780 回答