• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Flaw in Program

 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic