class Array
def total_zeros index = entries.length-1, total = 0
p "total entries = #{entries.length}"
i = 0
if index >= 0
p "in if block"
i = i+1
total_zeros index-1, total
end
p "hello #{i}"
end
end
a = [0,1,2]
p a.total_zeros
这是我的示例输出:
"total entries = 3"
"in if block"
"total entries = 3"
"in if block"
"total entries = 3"
"in if block"
"total entries = 3"
"hello 0"
"hello 1"
"hello 1"
"hello 1"
nil
有人可以帮我理解这段代码吗?我不明白为什么最后一行“hello”被打印了四次。这是一个递归,“hello”应该只打印一次,i
等于 4。