使用我的代码,我得到10以下数字的正确总和,但是当我的数字低于1000时,它似乎不正确。我不明白为什么简单地将10改为1000会导致我无法得到正确的解决方案。我得到了266333。
def multiplesofthreeandfive(number1, number2)
total1 = 0
total2 = 0
for i in 1..999
if i % number1 == 0
#puts "#{i}"
total1 = total1 + i
else
#nothing
end
end
for i in 1..999
#puts "#{i}"
if i % number2 == 0
#puts "#{i}"
total2 = total2 + i
else
#nothing
end
end
total3 = total1 + total2
puts "The 3 total is #{total1}"
puts "The 5 total is #{total2}"
puts "added together #{total3}"
end
puts multiplesofthreeandfive(3, 5)
答案 0 :(得分:2)
谢谢minitech。我没有正确地思考这个问题。我改变了我的代码,它运作了!
def multiplesofthreeandfive(number1, number2)
total1 = 0
total2 = 0
for i in 1..999
if i % number1 == 0 || i % number2 == 0
#puts "#{i}"
total1 = total1 + i
else
#nothing
end
end
total3 = total1 + total2
puts "The 3 total is #{total1}"
end
puts multiplesofthreeandfive(3, 5)