gpt4 book ai didi

ruby - while 循环小于符号 '<' 在 Ruby (RubyMine) 中引发错误

转载 作者:太空宇宙 更新时间:2023-11-03 17:11:26 25 4
gpt4 key购买 nike

我将所有小于 1000 的 3 和 5 的倍数相加。

这是我的错误:

in `multiple_sum': undefined method `<' for nil:NilClass (NoMethodError)

这是我的代码:

def multiple_sum(n)
five_total, three_total, three_subtract = 0
while five_total < n
five_total += five_total+5
end
while three_total < n
if (three_total+3)%5 == 0
three_subtract += three_total+3
end
three_total += three_total+3
end
puts (three_total-three_subtract) + five_total
end

multiple_sum(1000)

我的 while 循环条件有问题吗?

最佳答案

您可能正在寻找链式赋值:five_total = three_total = three_subtract = 0

关于ruby - while 循环小于符号 '<' 在 Ruby (RubyMine) 中引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16680707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com