Trying to encode 99 beer song bottles

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"); } } } 

Output:

 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 is the word String not equal to bottle? Instead, he says “bottles” in “Take one, skip it around 1 BOTTLE of beer.”

Also, after “1 bottle of beer on the wall, 1 bottle of beer”, she does not say: “Take one pass, go through the circle”

Link to the lyrics .

+7
java
source share
7 answers

Just put this piece of code that you wrote after you subtract the bottle number, not 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"); } } } 
-one
source share

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"); } } } } 

He will work with 1 bottle of beer at the exit of the wall. To fix 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 System.out.println("************") because it will give a clear idea when one loop ends and the other starts.

+5
source share

Try it, 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"); } } } 
+1
source share

A simple solution using a for loop for bottle song 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."); } } } } 
0
source share

No no no. This is definitely less mysterious than necessary. Go with my decision;)

 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;)

 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
source share
  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"); } } } } 
0
source share

my version of the recursive approach to the BeerSong program.

 public class BearSong { public static void main(String [] arg) { beer(99); } public static void beer(int n) { System.out.println(n + " bottles of beer on the wall"); System.out.println(n + " bottles of beer"); System.out.println("ya\' take one down, ya\' pass it around,"); lessBottle(n-1); } public static void lessBottle(int x ) { System.out.println( x + " bottles of beer on the wall."); System.out.println(); beerLess(x); } private static void beerLess(int x) { if (x > 0) { beer(x); }else { System.out.println("No bottles of beer on the wall"); System.out.println("no bottles of beer,"); System.out.println("ya' can't take one down, ya' can't pass it around,"); System.out.println("'cause there are no more bottles of beer on the wall!"); } } 

}

-one
source share

All Articles