8
public class apples {
    public static void main(String[] args) {
        int beerNum = 99;
        String word = "bottles";

        while (beerNum > 0) {

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

            System.out.println(beerNum + " " + word + " of beer on the wall, " + beerNum + " " + word + " of beer");
            beerNum = beerNum - 1;

            if (beerNum > 0) {
                System.out.println("Take one down, pass it round " + beerNum + " " + word + " of beer");
            }
        }

        if (beerNum == 0) {
            System.out.println("No more bottles of beer");
        }

    }
}

The output is:

99 bottles of beer on the wall, 99 bottles of beer
Take one down, pass it round 98 bottles of beer
98 bottles of beer on the wall, 98 bottles of beer
Take one down, pass it round 97 bottles of beer
97 bottles of beer on the wall, 97 bottles of beer
Take one down, pass it round 96 bottles of beer
96 bottles of beer on the wall, 96 bottles of beer
Take one down, pass it round 95 bottles of beer
95 bottles of beer on the wall, 95 bottles of beer... 

(And so on and so forth)

3 bottles of beer on the wall, 3 bottles of beer
Take one down, pass it round 2 bottles of beer
2 bottles of beer on the wall, 2 bottles of beer
Take one down, pass it round 1 bottles of beer
1 bottle of beer on the wall, 1 bottle of beer
No more bottles of beer

Why isn't the String word equalling "bottle"? Instead it says "bottles" in "Take one down, pass it round 1 BOTTLES of beer.

Also after "1 bottle of beer on the wall, 1 bottle of beer" it doesn't say "Take one down pass it round"

Link to the lyrics.

5
  • Because when you decide on the word, beerNum is not equal 1, but when you output Take one down, pass it round 1 bottles of beer, beerNum is 1, but you've already decided what the word should be... Sep 15, 2015 at 1:24
  • In words, describe what happens when beerNum is down to a value of 2. Sep 15, 2015 at 1:25
  • In other words, move the decrement to the end of the loop. Also move the check for bottles equals 1 to after that. Sep 15, 2015 at 1:27
  • Okay that worked, let me just have a quick think about why I had an error Sep 15, 2015 at 1:29

7 Answers 7

5

Try this code:

public class BeerSong{
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");
            System.out.println("***************************");
        }else {
            System.out.println("No more bottles of beer on the wall");
        }
    }
}
}

It will run with the 1 bottles of beer on the wall output. To correct this code 100%
Just move the if statement

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

after

beerNum = beerNum - 1;

Like this

public class BeerSong{
public static void main (String[] args){
    int beerNum = 99;
    String word = "bottles";
    while(beerNum > 0){
        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 == 1){
            word = "bottle";
        }
        if (beerNum > 0){
            System.out.println(beerNum + " " + word + " of beer on the wall");
            System.out.println("***************************");
        }else {
            System.out.println("No more bottles of beer on the wall");
        }
    }
}
}

I use the System.out.println("************") because it will give a clear idea when one loop ends and other starts.

1

Try this it will work..

public class beersong
{

    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");
            }
            else {
                System.out.println("no more bottles of beer on the wall");
            }
    }

}
0

Just put this piece of code you wrote after you subtract the bottle number instead of before

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

So your code will be

public class apples {
    public static void main(String[] args) {
        int beerNum = 99;
        String word = "bottles";

        while (beerNum > 0) {

            System.out.println(beerNum + " " + word + " of beer on the wall, " + beerNum + " " + word + " of beer");
            beerNum = beerNum - 1;

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

            if (beerNum > 0) {
                System.out.println("Take one down, pass it round " + beerNum + " " + word + " of beer");
            }
        }

        if (beerNum == 0) {
            System.out.println("No more bottles of beer");
        }
    }
}
2
  • 2
    Because you are checking if the value ==1 and then you subtract later on. So say you have 2, the if(beerNum == 1) will return false, but then it will subtract 1 from the 2. So your new beerNum is 1. You need to check again if the value == 1. You know the value has changed but your program doesnt. Sep 15, 2015 at 1:32
  • Dont forget to click the check mark to approve your question. Sep 15, 2015 at 1:35
0

A simple solution using for loop for bottle song is below :

public class apples {
        public static void main(String[] args) {
            for (int i = 99; i >= 0; i--) {
                if (i > 1) {
                    System.out.println(i + " bottles of beer on the wall, " + i + " bottles of beer.");
                    System.out.println("Take one down and pass it around, " + (i - 1) + " bottles of beer on the wall.");
                } else if (i == 1) {
                    System.out.println(i + " bottle of beer on the wall, " + i + " bottle of beer.");
                    System.out.println("Take one down and pass it around, " + (i - 1) + " bottle of beer on the wall.");
                } else if (i == 0) {
                    System.out.println("No more bottles of beer on the wall, no more bottles of beer.");
                    System.out.println("Go to the store and buy some more, 99 bottles of beer on the wall.");
                }
            }
        }
    }
1
  • I haven't got to else ifs in my book yet, started it today :D Sep 15, 2015 at 1:35
0

No, no, no. This is definitely less cryptic than it should. Go, with my solution ;)

public class Main {
  static String btl(int i) { return i > 1 ? " bottles of beer" : " bottle of beer"; }
  public static void main(String [] arg) {
    String [] btls = { " on the wall, ", "\nTake one down, pass it round, " };
    for(int i=99;i>0;i--) System.out.println("" + i + btl(i) + btls[0] + i + btl(i) + btls[1] + ((i>1)?i+btl(i):"no more bottles of beer"));
  }
}

and it works like a charm ;)

....
4 bottles of beer on the wall, 4 bottles of beer
Take one down, pass it round, 4 bottles of beer
3 bottles of beer on the wall, 3 bottles of beer
Take one down, pass it round, 3 bottles of beer
2 bottles of beer on the wall, 2 bottles of beer
Take one down, pass it round, 2 bottles of beer
1 bottle of beer on the wall, 1 bottle of beer
Take one down, pass it round, no more bottles of beer

And, you can always go with recursive calls as well ;)

public class Main {
  static String [] btls =  { " on the wall, ", "\nTake one down, pass it round, "  };
  static String btl(int i) { return i > 1 ? " bottles of beer" : " bottle of beer";}
  static int drink(int i)  {
    System.out.println(""+i+btl(i)+btls[0]+i+btl(i)+btls[1]+((i>1)?i+btl(i):"no more bottles of beer"));
    return (i>1)?drink(i-1):0;
  }
  public static void main(String [] arg) { drink(99); }
}
0
    public class BeerSong
    {
        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>1)
                {
                    System.out.println(beerNum+" "+word+" of beer on the wall");
                }
                if(beerNum==1)
                {
                    word = "bottle";
                    System.out.println(beerNum+" "+word+" of beer on the wall");
                }
                else 
                {
                    System.out.println("NO more bottles of the beer on the wall");
                }
            }
        }
    }
1
  • 1
    While this may answer the question, it is better to explain the essential parts of the answer and possibly what was the problem with OPs code.
    – pirho
    Dec 11, 2017 at 7:14
0

I came up with this solutions also public class Main {

public static void main(String[] args) {
  int beerNum = 1;
  String  word = "bottle";

  while (beerNum < 100) {
      if (beerNum == 1) {
          word = "bottle"; //singular, as in one 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;
  }
    System.out.println("No more bottles of beer on the wall");

}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.