Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Head First Into Java : 99 bottles of beer song Question

 
Marshal
Posts: 72657
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote the program this way:

as I felt of not using a different variable & using loop for singular and plural just for one line.
 
Campbell Ritchie
Marshal
Posts: 72657
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags to your post; you sho‍uld always use them, because: don't they make the code look better. Unfortunately, if your lines get any longer, they will become difficult to read.
Your different version goes to show that in programming, there are always several different ways to do the same thing. I presume it works
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but  i am still novice to do a post!
 
Saloon Keeper
Posts: 8095
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must add code tags. Go back and edit your code, highlight (click and drag) your whole code, then click on  the "code" button just above the editor screen.
 
Campbell Ritchie
Marshal
Posts: 72657
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too late! I have added code tags for him because he is new. Read all about them here. Doesn't it look better

Do you really want the bit about == 0 inside the loop?

And welcome to the Ranch.
 
Aek Ole
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i beg your pardon. Thank you for the exellent supporting!
 
Aek Ole
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
According to the below code why does the statement in the last 'if loop' execute first and then the print statements above that.
According to coding ethics line by line execution is allowed or is it that while loop executes all the if/for loops first then comes back to print statements.
I have understood what is the flaw anyway but please answer my question in the above lines.
Please comment ASAP

public class BeerSong
{
 public static void main (String [] args)
 {
 
 int beerNum =99;
 String word = "bottles";
 
   while (beerNum >0)
   {  
     if (beerNum == 1)
     {
 
       word = "bottle"; //singular, as in ONE bottle.
 
     }//end if statement
 
     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");
     }//end second if statement
     else
     {
       System.out.println("No more bottles of been on the wall");
     }//end else statement
 
    }//end while loop
 
 }//end main program
}//end class BeerSong
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just moved the if ==0 statement down below so it would read "1 bottle" instead of "1 bottles", and added a  System.out.println() to space the verses better.
 
Campbell Ritchie
Marshal
Posts: 72657
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DR: welcome to the Ranch

That example shows that there are different ways one can write loops and still get the program to work correctly.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess, I have solved it. If I am wrong, just correct me where should I fix thanks!

[size=25]

[/size]
 
Campbell Ritchie
Marshal
Posts: 72657
317
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

What happened when you ran your code? Please use the code tags on their own; you can't combine them with other sorts of tag.
 
Prasanta Kumar Saha
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

What happened when you ran your code? Please use the code tags on their own; you can't combine them with other sorts of tag.



100 bottles of beer on the wall,
100 bottles of beer!
Take one down.
Pass it around.
99 bottles of beer on the wall!
99 bottles of beer on the wall,
99 bottles of beer!
Take one down.
Pass it around.
98 bottles of beer on the wall!

1 bottle of beer on the wall!
1 bottle of beer on the wall,
1 bottle of beer!
Take one down.
Pass it around.
No more bottles of beer on the wall!
 
Campbell Ritchie
Marshal
Posts: 72657
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't that correct? You can improve formatting with a System.out.println(); call at line 13 and maybe a few spaces added to some of the lines to improve indentation, but it seems all right to me
Work out how to change “one” to “it” in lline 10 the last time round. Work out what the following means:-Work out what the following meansWork out whether I have got the format right.

[edit]Correct .. → .
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got a problem where my output only shows 49 bottles and below, it doesn't start form 99. Please help.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic