Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Seeking Guidance On my beginner code

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Seeking Guidance On my beginner code

    hello I am doing this excercise from my book and my code keeps ending up in a never ending loop and I can not figure out why.
    I know the right answer to the assignment, but I was wondering if my way has a possibility to work. thank you!

    ----Exercise 4.3. The first 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(finally) 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.--------




    public class BeersOnTheWall {
     
    	//Print statement for countdown
    	public static void beersOnWall(int OGbeers) {
    		System.out.println(OGbeers+" bottles of beer on the wall,");
    		System.out.println(OGbeers+ " bottles of beer, ya' take one down pass it around, "+ (OGbeers-1) +" bottles of beer." );
    		beersOnWall(OGbeers-1);
     
     
     
    	}
     
    	//print statement for End of loop
    	public static void noMoreBeers() {
    		System.out.println("No bottles of beer on the wall, no bottles of beer, ya’ can’t take\r\n" + 
    				"one down, ya’ can’t pass it around, ’cause there are no more\r\n" + 
    				"bottles of beer on the wall!");
    	}
     
    	//If statement begins here
    	public static void song(int OGbeers) {
    		if (OGbeers > 0){
    			beersOnWall(OGbeers);
     
     
     
    		}
    		else {
    			noMoreBeers();
    		}
    	}
     
    	//Main initilaztion
    	public static void main(String[] args) {
    		song(3);
    Last edited by brianp56; July 23rd, 2019 at 03:21 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Seeking Guidance On my beginner code

    code keeps ending up in a never ending loop
    That is usually because the condition to end the loop is never reached.
    Where is then loop you are talking about?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    brianp56 (July 22nd, 2019)

  4. #3
    Junior Member
    Join Date
    Jul 2019
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Seeking Guidance On my beginner code

    the loop starts at the if statement, in my head the way i see it working is that it will test if OGbeers is higher than 0 if so it will print the statement in beersOnWall and then minus one from OGbeers at the end of that
    then it will go back into the if statement and retest OGbeers until it is zero, but I dont think the if statement is retesting OGbeers after executing beersOnWall... Idk this probably sounds confusing but that is just my logic.

  5. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Seeking Guidance On my beginner code

    You have to call song() again inside beersOnWall method.
    Whatever you are, be a good one

  6. The Following User Says Thank You to John Joe For This Useful Post:

    brianp56 (July 23rd, 2019)

  7. #5
    Junior Member
    Join Date
    Jul 2019
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Seeking Guidance On my beginner code

    public class BeersOnTheWall {
     
    	//Print statement for countdown
    	public static void beersOnWall(int OGbeers) {
    		System.out.println(OGbeers+" bottles of beer on the wall,");
    		System.out.println(OGbeers+ " bottles of beer, ya' take one down pass it around, "+ (OGbeers-1) +" bottles of beer." );
    		song(OGbeers-1);
     
     
     
     
    	}
     
    	//print statement for End of loop
    	public static void noMoreBeers() {
    		System.out.println("No bottles of beer on the wall, no bottles of beer, ya’ can’t take\r\n" + 
    				"one down, ya’ can’t pass it around, ’cause there are no more\r\n" + 
    				"bottles of beer on the wall!");
    	}
     
    	//If statement begins here
    	public static void song(int OGbeers) {
    		if (OGbeers > 0) {
    			beersOnWall(OGbeers);
     
     
    		}
    		else {
    			noMoreBeers();
    		}
    	}
     
    	//Main initilaztion
    	public static void main(String[] args) {
    		song(99);

    Wow, hell ya! thank you so much! as to if this is the best way to write this sort of code, I think not, but I am glad to finally get it working! thank you again

    --- Update ---

    litterally had to switch

    beersOnWall(OGbeers-1);

    with song(OGbeers-1); and It worked.. Crazy how long I stared at it just flabbergasted lol

  8. #6
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Seeking Guidance On my beginner code

    No problem and all the best
    Whatever you are, be a good one

Similar Threads

  1. Replies: 0
    Last Post: November 10th, 2018, 06:52 PM
  2. need guidance
    By exuberent025 in forum Member Introductions
    Replies: 2
    Last Post: September 5th, 2014, 03:07 PM
  3. Guidance/Help with Code CleanUp
    By sternfox in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 6th, 2013, 11:02 AM
  4. Beginner seeking help with quick problem in program
    By Gravs2889 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: February 23rd, 2012, 11:59 PM