2

我在这一行有这个错误(if change_needed - coins[i] >= 0 then

字符串不能被强制转换为 Fixnum

从我的代码中,我不明白为什么它现在不起作用。任何有关如何最好地解决此问题的帮助将不胜感激。

user_input = gets
change_needed = 0
coins = []

coins_part, change_needed_part = user_input.split(':')
coins = coins_part.split(',')
change_needed = change_needed_part.to_i

i = 0
coins_used = []

while change_needed != 0 do
  if change_needed - coins[i] >= 0 then
    change_needed = change_needed - coins[i]
    coins_used << coins[i]
  else
    i += 1
  end
end
4

1 回答 1

4

coins是 , 的数组String,而您正尝试对 . 进行算术运算coins[i]

改变

coins = coins_part.split(',')

coins = coins_part.split(',').map(&:to_i)
于 2015-08-02T12:49:58.823 回答