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 Java Basics Perfecting the Prototype Looping until the value passes

Tiffany Der
Tiffany Der
556 Points

I watched the video on do while loops with puttputtgolf and can't translate the concept to make sense.

I watched the webinar on do while loops and their syntax was this below. However, I'm not quite understand what this question is asking me and how I translate that concept of do while loops from puttputt golf into a line that keeps on asking "Do you understand do while loops?"

I get the do while syntax should be:

do {

} while ();

} }

but i don't understand the whole concept as to what we are doing in there. from my understanding, we are asking them "Do you understand do while loops?" take the string variable out and put it in the do section?....I am just at a loss. so if anyone could please explain to me not only the syntax but a deeper understanding of what this is trying to do (perhaps also in psuedo ) that would be so helpful becauseI am beyond frustrated and I feel like some of this material is skipping ahead assuming I know so much already when I don't....I've spent over 2 hours studying just loops already and still have no clue.

-------------------------(the code from MiniGolf.java)

public statis void main (String[] args) { Console console = System.console(); Random luck = new Random();

int numberOfPutts = 0;
boolean ballInHole = false;

do {

console.printf("Putt putt %n")
  ballInHole = luck.nextBoolean();
  numberOfPutts++;

} while(!ballInHole);

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

} }

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
String response = console.readLine("Do you understand do while loops?");

  do { String question = console.readLine("Do you understand do while loops? ");
      String response = question; if(response.equalsIgnoreCase("no."));

{ while(response ); console.printf("try again. \n");

}

}

1 Answer

Steven Parker
Steven Parker
231,269 Points

Here's a few hints:

  • parentheses, braces, and quotes should always be used in matched pairs
  • an advantage of the "do...while" loop is the question only needs to be asked once (inside the loop)
  • an "if" statement followed by a semicolon doesn't do anything
  • the condition to determine when to end the loop should be tested in the "while" expression
  • the challenge doesn't ask for a "try again" message
  • the period ends the sentence and isn't intended to be part of the "No" string