| Author |
Newbie Q: Head First Into Java : 99 bottles of beer song Question
|
Jimmy Semaj
Greenhorn
Joined: Jun 24, 2007
Posts: 3
|
|
Ok so I am quite new to the java programming language (as well as programming in general) so i picked up the Java Head First book for JAVA 5. Going through the examples i am having problems correcting the "99 bottles of beer" problem on page 14 of the second edition of the book. The code compiles and works but there is a snippit below that says " there's still one little flow in our code. It runs, but it's output isn't 100% perfect. See if you can spot the flaw, and fix it.". I have been working on it for the better part of a half an hour and i cannot fix it. I believe the problem is at the 1 bottle of beer part of the output. Daniel Morrison mentioned in and earlier post:
Yes, and thanks. I also found the other flaw put there by the author: code: if (beerNum == 1) { word = "bottle"; //single bottle of beer } should come after... code: beerNum = beerNum - 1;
I must be doing something wrong since it does not seem to work for me. The following code is before i started screwing it up. My last rendition actually froze up the compiler for a long while. I have been compiling in netbeans ide5.5 since the command line does not seem to want to always compile and the IDE can somewhat help me find my mistakes. Mistakes i have found have been normally spelling. [ June 24, 2007: Message edited by: semaj semaj ]
|
 |
Jimmy Semaj
Greenhorn
Joined: Jun 24, 2007
Posts: 3
|
|
I am not sure what I may have been missing but i started fresh and the code worked this time. Maybe this topic can be used for reference for people.
|
 |
Dhawal Mehta
Greenhorn
Joined: Jun 13, 2010
Posts: 6
|
|
Hey Everybody.
Jimmy is right. we have to place the first If loop after statements ans the output got correct.
One thing more I noticed that when control enter while loop it first go to the If loop and print the statement under this loop first:
if (beerNum > 0) {
System.out.println(beerNum + " " + word + " of beer on the wall");
}
and the goes to the while loop statements. Am i correct?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
Welcome to the Ranch Dhawal Mehta
|
 |
Dhawal Mehta
Greenhorn
Joined: Jun 13, 2010
Posts: 6
|
|
Thanks a lot for your warm welcome Campbell..
May you please check my previous post and tell me if i am correct....
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
I presume you have run that code and verified that it is correct?
|
 |
Dhawal Mehta
Greenhorn
Joined: Jun 13, 2010
Posts: 6
|
|
Yes .. I have run this code already...
but what i found is that when the control goes to 'while' loop.. it first executes the most inner 'if' loop and then execute the other instructions..
Is that correct?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
Don't understand; when control enters the while loop, the first lines inside it are executed first, not the innermost if.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
By the way: there doesn't appear to be an inner if nor an outer if in that example; the two ifs are adjacent, not nested.
|
 |
shafkat majeed
Greenhorn
Joined: Mar 12, 2013
Posts: 1
|
|
public class BeerSong{
public static void main(String args[]){
int beerno=99;
String name="bottles";
while(beerno>1){
System.out.println();
System.out.print(beerno + " " + name + " " + "of bear on the wall,");
System.out.println(beerno + "" + name + " " +"of bear" );
System.out.print("take one down,pass it around,");
beerno--;
if(beerno>1)
System.out.println(beerno + " " + name + " " + "of bear on the wall");
else
{
name = "bottle";
System.out.println( beerno + " " + name + " " + "of bear on the wall");
}//end if
}//end loop
System.out.print(beerno + " " + name + " " + "of bear on the wall,");
System.out.println(beerno + " " + name + " " + "of bear" );
System.out.println("take one down,pass it around,");
System.out.print("no more bottles of bear on the wall");
}//end main
}//end class
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
Welcome to the Ranch
Any point in answering a three‑year‑old thread?
|
 |
 |
|
|
subject: Newbie Q: Head First Into Java : 99 bottles of beer song Question
|
|
|