0
public class Head1 {
  public static void main(String[] args) {
    int beerNum = 99;
    String word = "bottles";
    while (beerNum > 0) {
      if (beerNum == 1) {
        word = "bottle";
      }
      System.out.println(beerNum + " " + word + " of beer on the wall");
      System.out.println(beerNum + " " + word + " of beer");
      System.out.println("Take one down.");
      System.out.println("Pass it around.");
      beerNum = beerNum - 1;
      if (beerNum > 0) {
        System.out.println(beerNum + " " + word + " of beer on the wall");
      }
      if (beerNum == 1) {
        System.out.println(beerNum + " " + word + " of beer on the wall");
      } else {
        System.out.println("No more bottles of beer on the wall!");
      }
    }
  }
}

This example code from a Java book prints out the song from 99 bottles to no bottles of beer on the wall. The problem is that when it is 1 bottle of beer on the wall, it still says bottles. I tried to fix this by adding if (beerNum == 1) section at the end. But still, it shows 1 bottles of beer on the wall, i bottle of beer on the wall.

I don't know what to change to fix this. Do I create another while section?

If you can give em a hint so I can solve it on my own that would be cool too! Because I do understand that I the actual song output is in the first if section, but I don't know where I should edit "if" or if i should just create another if section.

Thanks!

4
  • 2
    This question shows lack of basic java knowledge... Commented Apr 10, 2013 at 22:11
  • 2
    Thats why I'm learning -_- Commented Apr 10, 2013 at 22:21
  • @Menelaos I dunno there's specific problem, relavant source code and good explanation what he has tried and what he is trying to do. It's well above the average question here :-) Commented Apr 10, 2013 at 23:20
  • @Esailija True, when you say it like that. Probably only title is too generic. Commented Apr 11, 2013 at 7:05

2 Answers 2

4

You update beerNum and then print it out. Put the section

if (beerNum == 1) {
    word = "bottle";
}

after the line where you update the value of beerNum. Using separate variables for "bottle" and "bottles" would also be a good idea.

Sign up to request clarification or add additional context in comments.

1 Comment

Took me a bit longer then it should but I get it now! Thanks guys!
0

You can also do the same without loops and use recursion.

public class Bottles {
    public static void main(String[] args) {
        removeBottle(100);
    }

    private static void removeBottle(int numOfBottles) {
        // IF the number of bottles is LESS THAN OR EQUAL to 1 print singular version
        // ELSE print plural version
        if (numOfBottles <= 1) {
            System.out.println(numOfBottles + " bottle of beer on the wall.");
        } else {
            System.out.println(numOfBottles + " bottles of beer on the wall.");
        }

        // print of the rest of song
        System.out.println("Take one down.");
        System.out.println("Pass it around.\n"); // "\n" just puts new line

        numOfBottles--; // remove a bottle

        // IF the number of bottles is GREATER THAN OR EQUAL to 1 do it again!
        // ELSE no more bottles =(
        if (numOfBottles >= 1) {
            removeBottle(numOfBottles);
        } else {
            System.out.println("No more bottles of beer on the wall!");
        }
    }
}

6 Comments

That's a good way to get a stack overflow though. I personally find loops more readable too, in this particular case.
Ahhh that's a dif approach altogether, I already solved it, but thanks anyways!
@LearnIT, its good to keep it in mind, will come handy later on.
@RaptorDotCpp, it will only cause a stack overflow if you don't terminate it properly. its basically like a while-loop. I do agree loops are easier to read, but I think recursion in some situation are faster.
Or if your stack grows too big. Ofcourse that wouldn't happen here. Not on any modern machine.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.