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 Do While Loops

Bhanu Awasthi
Bhanu Awasthi
1,780 Points

Confused about the program written by Craig!

Console console = System.console(); Random luck = new Random(); // What is this and what this do ? //

int numberOfPutts = 0;
boolean ballInHole = false;            // Why boolean set to False ?//

do {
  console.printf("Putt putt %n");
  ballInHole = luck.nextBoolean();  // Why is ballinHole= luck.nextBoolean ?//
  numberOfPutts++;
} while(!ballInHole);

console.printf("You got it in %d putts %n", numberOfPutts);

4 Answers

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey There.

So all that this code is essentially giving the boolean a 50% chance to be positive.

The first thing he does is declare the Random luck. Which is a pretty nifty class that I've included the javadoc to.

He's first declaring the Boolean to false so that the code can run in the do while loop.

Next what happens is he changes Boolean to a random value, using the method on the Random object getBoolean() which is going to return one of two values.

As long as the value returns false, we increase the number of putts, and run the code again, until ballInHole is randomly changed to true, then the code stops and it gives us back the number of puts it took (Or how many times the code was looped in our while loop)

Thanks hope this helps, shoot back if it doesn't.

Maxamed Dool
Maxamed Dool
9,439 Points

Hi Catherine,

Good questions. First, the boolean value (false or true) is set depending on how you want the condition being tested to start out, in this case, while the ball is not in the hall (hence, a false) keep doing the code within the braces.

Second: NumberOfPutts is a counter, and while counters can start from any number you wish, this count starts from zero becuase it counts the number of strikes on the ball.

Thirdly, on how NumberOfPutts keeps track - simply, first of all, the lines inside the brace are implemented sequentially, hence, it counts every time it did around, and breaks when the condition is true and returns the number of times it went round.

Hope that clears.

best, d

Bhanu Awasthi
Bhanu Awasthi
1,780 Points

Hi Rob,

Sorry for the late revert on the answer to my question.I resumed my learning after almost 2 weeks.

You have made me clear about whole program. I am pretty much clear now.

Thanks for the help.

Best Regards,

catherine paris
catherine paris
2,705 Points

Rob I kind of understand. Can you explain it again? Does the boolean always have to be false to run in the do while loop? Why is NumberofPutts set to 0? If the program resets until it randomly has the ball in the hole how does it know to keep track of the number of attempts???