This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Head First Java Q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java Q" Watch "Head First Java Q" New topic
Author

Head First Java Q

andrew dale
Ranch Hand

Joined: Aug 15, 2011
Posts: 45
I saw that this question was already asked on the forums and answered a year ago, but it didn't answer my question too it....


Let me post the code and then I will ask the questions about it



Ok First, the beer = beernum -1, I understand

But below that the next line is if {beernum ==1){
word = bottle,

That was originally below the first while () you see in the code, why did it have to be moved down (it said that was apart of the problem in the first post)

Secondly this code is starting me from 2 beer bottles and going down from there... Why?

It runs and ends perfect but starts at 2, if you could answer these questions it would be greatly apprecaited.

And I am excited to be apart of these forums as I learn Java!


You'll never achieve your goals if you don't take that chance, so go pry open that trunk and get those amps.
andrew dale
Ranch Hand

Joined: Aug 15, 2011
Posts: 45
Also sorry,

How does the compuer know that when I type , that I also want it to continuously go down from 99? I thought that the computer would think that the code just ment 99-1 post the number and end.

and when it does get to 98, how does it know to pickup from the last number that it posted itself? I feel like this code isint telling it to do it, but it does it somehow..
andrew dale
Ranch Hand

Joined: Aug 15, 2011
Posts: 45
Nevermind the code works perfect - Just did scroll up in the test run, only saw 1 bottle text and didn't think it counted from 99.
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

andrew dale wrote:
Ok First, the beer = beernum -1, I understand

But below that the next line is if {beernum ==1){
word = bottle,

That was originally below the first while () you see in the code, why did it have to be moved down (it said that was apart of the problem in the first post)

Secondly this code is starting me from 2 beer bottles and going down from there... Why?

It runs and ends perfect but starts at 2, if you could answer these questions it would be greatly apprecaited.

And I am excited to be apart of these forums as I learn Java!


First answer would be that else the ouput would be wrong if you are at 1.
You would make a decrement at 2 bottles, which is 1 bottle. give it out with 1 bottles and check after that if its 1 bottle.

No idea why this code starts from 2 for you. Should run from 99. Tried it and was correct.

Second Post:

The computer simply computes what you told him.

beernum= beernum-1;
means you got 99. decrement it by one, which is 98 and then overwrite the var beernum with 98.
In the next loop you got 98 decremented by 1= 97. beernum=97;
You could also write:
int help=beernum;
beernum=help-1;

if that would be more convenient for you.

Please correct my English.
andrew dale
Ranch Hand

Joined: Aug 15, 2011
Posts: 45
Thank you for the response Manuel, greatly appreciated.
 
 
subject: Head First Java Q