Java Java Basics Perfecting the Prototype Looping until the value passes

Got stuck

How can I fixed this? What is an error?

Thank you

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

      if (understand) {
        console.printf("Try again.");
      }
} while (understand);
  String gotIt = console.readLine("Yay!");

1 Answer

Tonnie Fanadez
Tonnie Fanadez
UX Design Techdegree Graduate 22,778 Points

Hi Aethaya Meesawat

Thanks for your query.

First of all you should declare the String response and initialize it to input from the Console Object. do-while loop is used to execute a block of statements continuously until the given condition is true. In this case, you should continuously prompt the user if he or she understands the loop until true is returned. The do-while at least runs once and the expression inside the while(expression) must return a boolean value so you don't need to introduce your own boolean value.

Lastly, if the condition tests true you should print out the last statement saying you passed. you can use the %s String Modifier to represent the String.

Please see the code below, hope it helps.

// 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 {
response = console.readLine("Do you understand do while loops");
  console.printf("Because you said %s, you passed the test!", response);
}
while(response.equals("No"));