The moose likes Beginning Java and the fly likes 99 Bottles of Beer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "99 Bottles of Beer" Watch "99 Bottles of Beer" New topic

99 Bottles of Beer

loni gashi

Joined: Dec 11, 2012
Posts: 11
Hi, I'm new here and also new in java. I'm reading Head First Java second edition and
arrived at exercise 99 Bootes of beer. I wrote this code but the output isn't correct. Can anyone explain
to me why isn't working as expected ( x doesn't take value 100 and then degrease by 1 , but it take value 0 and then decreases)

Greg Charles

Joined: Oct 01, 2001
Posts: 2840

Hi Loni, and welcome to JavaRanch!

It's a good idea to tell us what's not working to your expectations. It's explained in our FAQ under: TellTheDetails ⇐ (Click here). In this case, I think I can guess that the numbers are going down faster than you want them to. Is that right? You are subtracting from x too often. Just once in the loop is enough.
Kemal Sokolovic

Joined: Jun 19, 2010
Posts: 825

A similar question has already been asked here so you might find some useful information there.

The quieter you are, the more you are able to hear.
loni gashi

Joined: Dec 11, 2012
Posts: 11
Thanks for quick replies.
I realized that i made an stupid mistake, The command should have been inside while brackets
Kemal Sokolovic I saw that post but I tried to make it shorter and achive the same result. Now it is working as it should.
Thanks again.

Kemal Sokolovic

Joined: Jun 19, 2010
Posts: 825

I've notices that your solution is slightly different from that one, but I posted a link as a guideline you might use.

1 bottles of beer on the wall, 1 bottles of beer.
Take one down and pass it around, 0 bottles of beer on the wall.

This is the output of your program. The last line differs a little from the one I know about.
Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 231

I remember this from a while back: http://www.coderanch.com/t/409007/java/java/Head-Beersong
I agree. Here's the link: http://aspose.com/file-tools
subject: 99 Bottles of Beer
Similar Threads
Head first Beersong
quick question from the book Head First Java
Java Beginner
BeerSong Java code