# Thread: recursive 99 bottles program is there better way to write it as a beginner?

1. Member
Join Date
Sep 2012
Location
Sherburne, NY
Posts
6
Rep Power
0

## recursive 99 bottles program is there better way to write it as a beginner?

okay so just being a hmmm... what is the word beneath novice, anyways for a book exercise i had to right a program for the song 99 bottles of beer, this was involving recursive methods. it hinted around to more than one method. but my code only had one method and got what it wanted.
so my question is simple with out going above beginner level what is better way to write it as a recursive method the comment was for me to think about it i couldnt come up with anything different.
This is not homework i am 30 years old and learning java from a book and the web, this is my first programming language i decided to learn
here is my code:

Java Code:
```public class nineninebottlesofbeer {
//book said maybe more methods, smaller problems broken down to different methods
public static void main(String[] args) {
//99 bottles of beer song
beer(10);
}
public static void beer( int x){
if(x>0){
System.out.print(x +" bottles of beer on the wall, ");
System.out.println(x +" bottles of beer, ");
System.out.println("ya'take one down and ya' pass it around");
System.out.println((x-1)+" bottles of beer on the wall");
beer(x-1);
}else{
System.out.println("No bottles of beer on the wall, no bottlesof beer ");
System.out.println("ya'can't take one down, ya can't pass it around,");
System.out.println("'cause there is no more bottles of beer on the wall");
}
}
}```

2. Member
Join Date
Sep 2012
Posts
77
Rep Power
0

## Re: recursive 99 bottles program is there better way to write it as a beginner?

I too am an Apprentice :) A little older but also trying to learn Java self-taught. Just to share, I thought I would try out your idea of a recursive bottle of beer program.
Java Code:
```public class BeerSong{

public static void main(String[] args){

int beer = 99;

while(beer>0){
System.out.println(beer + " bottles of beer on the wall,");
System.out.println(beer + " bottles of beer,");
System.out.println("ya' take one down, pass it around");
beer--;
System.out.println(beer + " bottles of beer on the wall");
System.out.println();

}
if(beer==0){
System.out.println("No more bottles of beer you lush,");
System.out.println("No more bottles of beer");
System.out.println("You drank them all, and now you fall");
System.out.println("Cause you drank all your bottles of beer on the wall");
}
}
}```
Not sure if it was how you were thinking.

3. Member
Join Date
Sep 2012
Location
Sherburne, NY
Posts
6
Rep Power
0

## Re: recursive 99 bottles program is there better way to write it as a beginner?

that is kind what i was thinking but, the book i am reading hasn't covered while so i think i am going to have to look that up. hmm.... if you dont mind me asking what books/media are you using to teach yourself

4. Member
Join Date
Sep 2012
Posts
77
Rep Power
0

5. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12

## Re: recursive 99 bottles program is there better way to write it as a beginner?

@darkbandit, your code looks OK to me. Although classes should begin with an upper case letter NineNineBottlesOfBeer (this affects the name of the .java file as well).

If you think the book was hinting at something, post what it said as that might shed light on things. For what it's worth your code does use more than one method: main() and beer().

rru96's code is a good example of how you would do this without recursion. The "if(beer==0)" is not really needed because the while loop won't finish until the beer is.

6. Member
Join Date
Sep 2012
Posts
77
Rep Power
0

## Re: recursive 99 bottles program is there better way to write it as a beginner?

<--- Still not familiar with terminology and all that jazz. Thank you pbrock for pointing out the if(beer==0) redundancy. It makes a lot of sense that once the while loop is done, it continues on with the code, i.e. No more bottles.

7. Member
Join Date
Sep 2012
Location
Sherburne, NY
Posts
6
Rep Power
0

## Re: recursive 99 bottles program is there better way to write it as a beginner?

sorry its taken a little bit but here is what the book said:

"Exercise 4.3. The rst verse of the song \99 Bottles of Beer" is:
99 bottles of beer on the wall, 99 bottles of beer, ya' take one
down, ya' pass it around, 98 bottles of beer on the wall.
Subsequent verses are identical except that the number of bottles gets smaller
by one in each verse, until the last verse:
No bottles of beer on the wall, no bottles of beer, ya' can't take
one down, ya' can't pass it around, 'cause there are no more
bottles of beer on the wall!
And then the song( nally) ends.
Write a program that prints the entire lyrics of \99 Bottles of Beer." Your
program should include a recursive method that does the hard part, but you
might want to write additional methods to separate the major functions of
the program.
As you develop your code, test it with a small number of verses, like \3
Bottles of Beer."
The purpose of this exercise is to take a problem and break it into smaller
problems, and to solve the smaller problems by writing simple methods."

8. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12

## Re: recursive 99 bottles program is there better way to write it as a beginner?

Well I can't see anything obviously wrong with your code.

9. Member
Join Date
Sep 2012
Location
Sherburne, NY
Posts
6
Rep Power
0

## Re: recursive 99 bottles program is there better way to write it as a beginner?

hmm... cool thanks. guess i did it right awesome yeah

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•