Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Flaw in Program

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just started introducing myself to the Head First Java book. On page 14 there is a program as follows:



In the book it states that there is a flaw in the program but I can't see where it is as the output to screen I get is (which starts from 99 but I can't copy that far up from DOS screen):

6 bottles of beer on the wall
6 bottles of beer on the wall
6 bottlesof beer.
Take one down.
Pass it around.
5 bottles of beer on the wall
5 bottles of beer on the wall
5 bottlesof beer.
Take one down.
Pass it around.
4 bottles of beer on the wall
4 bottles of beer on the wall
4 bottlesof beer.
Take one down.
Pass it around.
3 bottles of beer on the wall
3 bottles of beer on the wall
3 bottlesof beer.
Take one down.
Pass it around.
2 bottles of beer on the wall
2 bottles of beer on the wall
2 bottlesof beer.
Take one down.
Pass it around.
1 bottles of beer on the wall
1 bottle of beer on the wall
1 bottleof beer.
Take one down.
Pass it around.
No more bottles of been on the wall

C:\Java>

Can anyone highlight what the flaw is?
 
Dani Atrei
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 bottles of beer on the wall


The output isn't perfect, I think that's all

Dan
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah! Because a while loop has been used it will go through it once and use the singular bottle. The if statement should of been outside the while loop then.

Thank you!
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maureen Charlton:
Ah! Because a while loop has been used it will go through it once and use the singular bottle. The if statement should of been outside the while loop then.

Thank you!


I don't think that will fix the problem, either. But you should try it and see what happens.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic