不能将字符串强制转换为 Fixnum
String can't be coerced into Fixnum
这一行我有这个错误(if change_needed - coins[i] >= 0 then
)
String can't be coerced into 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
coins
是 String
的数组,而你试图用 coins[i]
.
进行算术运算
改变
coins = coins_part.split(',')
到
coins = coins_part.split(',').map(&:to_i)
这一行我有这个错误(if change_needed - coins[i] >= 0 then
)
String can't be coerced into 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
coins
是 String
的数组,而你试图用 coins[i]
.
改变
coins = coins_part.split(',')
到
coins = coins_part.split(',').map(&:to_i)