[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Learn to Program, by Chris Pine

William (Bill) Froelich

3/31/2006 6:37:00 AM

How's this for a solution. Note I tried to only use constructs covered
up to the point of the exercise in the book.

numBottles = 99
while numBottles > 2
puts numBottles.to_s + " bottles of beer on the wall, " +
numBottles.to_s + " bottles of beer,"
puts "take one down, pass it around, " + (numBottles-1).to_s + "
bottles of beer on the wall!"
puts ""
numBottles = numBottles - 1
end
puts numBottles.to_s + " bottles of beer on the wall, " +
numBottles.to_s + " bottles of beer,"
puts "take one down, pass it around, " + (numBottles-1).to_s + " bottle
of beer on the wall!"
puts "Yay!"


--Bill

-----Original Message-----
From: JB [mailto:directly_above_the_center@of_the_earth.com]
Sent: Thursday, March 30, 2006 11:39 PM
To: ruby-talk ML
Subject: Re: Learn to Program, by Chris Pine

Jan_K wrote:

> Does anyone know where I can find the solutions to the exercises in
> this book:
>
> http://pine.fm/Learn...
>
>
> I'm stuck in "Flow Control" chapter, specificailly the "Extend Deaf
> Grandma" exercise.

I'm stuck trying to do the part to make it do '99 bottles of beer on
the wall' song. I get started in what I hope will work and next thing I
know I have way too many variables and nothing makes sense. I even did
something that made it loop the integer 0. I had to Ctrl-c on that one.
<sigh>

Here's what I quit trying at, because no matter how many breaks I took
or whatever, it just wasn't making any sense. :


puts '99 bottles of beer on the wall, 99 bottles of beer,'
'take one down, pass it around,'
'98 bottles of beer on the wall!'
song = '99 bottles of beer on the wall, 99 bottles of beer, take one
down, pass it around,'
song = 99
song2 = '98 bottles of beer on the wall!'
song2 = 99 - 1
last = '1 bottle of beer on the wall'
last = 1

if song2 == last
puts 'Yay!'
else
if song2 - 1
puts
while song == 1
end
endputs '99 bottles of beer on the wall, 99 bottles of beer,'
'take one down, pass it around,'
'98 bottles of beer on the wall!'
song = '99 bottles of beer on the wall, 99 bottles of beer, take one
down, pass it around,'
song = 99
song2 = '98 bottles of beer on the wall!'
song2 = 99 - 1
last = '1 bottle of beer on the wall'
last = 1
if song2 == last
puts 'Yay!'
else
if song2 - 1
puts
while song == 1
end
end


I too wish the author had put examples, say on some pages in the back
or something.




1 Answer

JB

3/31/2006 8:37:00 AM

0

William (Bill) Froelich wrote:

> How's this for a solution. Note I tried to only use constructs covered
> up to the point of the exercise in the book.
>
> numBottles = 99
> while numBottles > 2
> puts numBottles.to_s + " bottles of beer on the wall, " +
> numBottles.to_s + " bottles of beer,"
> puts "take one down, pass it around, " + (numBottles-1).to_s + "
> bottles of beer on the wall!"
> puts ""
> numBottles = numBottles - 1
> end
> puts numBottles.to_s + " bottles of beer on the wall, " +
> numBottles.to_s + " bottles of beer,"
> puts "take one down, pass it around, " + (numBottles-1).to_s + " bottle
> of beer on the wall!"
> puts "Yay!"
>
>
> --Bill

Wow! It was so simple once I saw how it should/could be done! I'd (for some
reason) completely forgot to do the '+ <whatever> +' thing in a 'puts'! I'm
terrible at learning things, having severe ADHD, so if you don't tire of
helping me, I won't tire of still trying to learn programming.

One question though about something that's got me still a little confused.
Why the 'while numBottles > 2' ?

Anyway, I really do appreciate the help Bill! Thank you very much!