Social networks
You can find me on:
   
Open sharing content

These articles are available under Creative Commons license BY-SA-3.0

Archive for November, 2011

Even though I’ve been into Java since 2007, I had never found out how recursion can be powerful when you have to print several lines of code…

So now I’ve just made a funny little programs with a recursive method.

Separating the cases by a switch, this funny Beer Ballad plays even better!

Check it out!

—————————————————–// BeerBallad.java”//———————




import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Beer {

public static void balladCounter(int n) {
switch(n){
case 0:  System.out.println("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! \n"); break;
case 1:  System.out.println("1 bottle of beer on the wall, 1 bottle of beer,  ya’ take one down, ya’ pass it around, and now there are no more bottles of beer on the wall! \n");
balladCounter(n-1);   break;
case 2:    System.out.println("2 bottles of beer on the wall, 2 bottles of beer,  ya’ take one down, ya’ pass it around, and now just 1 bottle of beer on the wall! \n");
balladCounter(n-1);     break;
default:     // if n>2
System.out.println(n+" bottles of beer on the wall, "+n+" bottles of beer, ya’ take one down, ya’ pass it around, "+(n-1)+" bottles of beer on the wall. \n");
balladCounter(n-1);    break;
}
}

public static void main(String[] args) {
int m = 0;   //input from console
String inputString = null;
BufferedReader bufferedReader = null;
System.out.println("How many beers do you want to sing about?");
bufferedReader = new BufferedReader(new InputStreamReader(System.in));

try { inputString = bufferedReader.readLine();


m = Integer.parseInt(inputString);

System.out.println("\n THE BALLAD OF THE BEER \n");
balladCounter(m); }
catch (IOException e) { e.printStackTrace();}
}
}