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 trial

Java Difficulties With Loops

Bug! You are allowing Jim 4 Balls!

int ballsThrown = 0; while (!isDunked && ballsThrown <= 3) { console.printf("Try #%d ...%n", ballsThrown + 1); //isDunked = skill.nextBoolean(); //ballsThrown++; }

In Difficulties With Loops, if Jim always misses, that is isDunked remains false, Jim will get 4 balls zeroth, first, second, third. Count them that is 4. The test should be changed to ballsThrown < 3. This is a common fence post error.

Tagging Craig Dennis

I don't agree. You just counted balls badly. 0 is not a throw. It is number of throws before game started. 1st throw is just counted inside the very first loop in the line >> ballsThrown++. So the second fail loop starts with ballThrown == 1 and ends with ballsThrown == 2. If the second throw misses, third fail loop begins with ballsThrown == 2 and ends with ballsThrown == 3. Finally, if this last throw missed again, the condition of the loop (!isDunked && ballsThrown <= 3) doesn't let grab 4th ball. Game Over.

2 Answers

you are right!

Yes it should ballsThrown <3, not ballsThrown <= 3.