This loop does not terminate after I type x. I’m really new to Ruby, and so far, it is so much different than what I learned before – quite interesting,
total = 0
i = 0
while ((number = gets) != "x")
total += number.to_i
i += 1
end
puts "\nAverage: " + (total / i).to_s
Any help is greatly appreciated.
Because
getsgives you the newline as well. You need tochompit.Try:
and you’ll see it starts working: