Saturday, July 14, 2012

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

This problem is on page 14th of head first. And above provided is the best solution for the problem.
Here is output:
Before fixing problem Output is:
99 bottle of beer on wall
99 bottle of beer
Take one down
Pass it around
98 bottle of beer on wall
98 bottle of beer on wall
98 bottle of beer
Take one down
Pass it around
97 bottle of beer on wall
97 bottle of beer on wall
97 bottle of beer
Take one down
Pass it around
 

After fixing Problem Output is:
99 bottle of beer on wall
99 bottle of beer
Take one down
Pass it around
98 bottle of beer on wall
98 bottle of beer
Take one down
Pass it around
97 bottle of beer on wall
97 bottle of beer
Take one down
Pass it around