Pregunta

I am scratching my head on this problem, I am new to ruby and have been following along with Chris Pines book. One exercise had us re-write the code for a "99 bottles of beer" program but this time using a method. The problem is when I run the code, if the parameter is anything greater than 255 I only get "255 bottles of beer". I have attached the code. This is the first method I’ve written so may not be the prettiest things.

def bottles num
if num < 0 # Making sure a positive is entered.
    return 'Please enter positive bottle count'
end
while num > 0 
    puts num.to_s + ' bottles of beer on the wall, ' + num.to_s + ' bottles of beer'
    num = num - 1 # Taking away a bottle.
    puts 'take one down, pass it around, ' + num.to_s + ' bottles of beer on the wall.'
end

if num == 0
    puts 'Ut-oh, out of beer...'
end
end

bottles 1000

So when this runs in terminal, the output would be like so:

255 bottles of beer on the wall, 255 bottles of beer take one down, pass it around, 254 bottles of beer on the wall. 254 bottles of beer on the wall, 254 bottles of beer take one down, pass it around, 253 bottles of beer on the wall.

Thanks!

¿Fue útil?

Solución

Perhaps the scroll back buffer is to small in your terminal. Check this out.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top