Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialyadin michaeli
Courses Plus Student 5,423 PointsChomp 12 time ??
Hi.
i dont really get it...how it know to say chomp 12 times? somebody can help me with this please?
4 Answers
Benjamin Sonin
12,938 PointsYou've got to remember that the while loop will execute until its statement is false. Dispense() is a method which returns a boolean false when the PezDispenser is empty. If it is full, a Pez is dispensed and the method returns true, so the while loop will keep going. I don't remember off the top of my head, but earlier in the code the Pez Dispenser is probably loaded with 12 candies. Thus, dispense will return true 12 times and you'll see Chomp! printed to the screen each time.
Benjamin Sonin
12,938 PointsBecause the number of pez in the PezDispenser is stored in a member variable, and the while loop in dispense will execute as long as that member variable is nonzero (so if you have 12 candies, the while loop executes 12 times).
yadin michaeli
Courses Plus Student 5,423 Pointsohhh now i got it thank you very much!
Benjamin Sonin
12,938 PointsHey Yadin,
Can you provide some code you're struggling with?
yadin michaeli
Courses Plus Student 5,423 PointsHi.
When we type this while (dispenser.dispense()) { System.out.println("Chomp!"); }
how it know to print 12 chomp?
yadin michaeli
Courses Plus Student 5,423 Pointsyadin michaeli
Courses Plus Student 5,423 PointsBut how (dispenser.dispense()) know there is a 12 chomp time?