小编Dun*_*nny的帖子

如何对数组中的数字求和

我试图用函数对数组中的数字求和:

def total(numbers)
 numbers.each do |e|
  total_number = e + total_number
  return total_number
 end
end

puts total([1,2,3,4])
Run Code Online (Sandbox Code Playgroud)

但我不断收到一条错误消息:

nil无法强制进入Fixnum(repl):18:在'+'

谁能看到我做错了什么?

each> do循环在函数之外工作.

ruby

-1
推荐指数
2
解决办法
127
查看次数

标签 统计

ruby ×1