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

catherine paris
catherine paris
2,705 Points

Don't understand how the program knows what to do if the answer is no. Can someone explain the code very simply???

console.printf("I do not understand: %s", question); anyQuestions = console.readLine("Are there any more questions %n ");

This specific part doesn't make sense to me.

1 Answer

The first line will print out "I do not understand: " then it will print the contents of the variable question (if it has been declared). The second line will take input from the user after prompting: "Are there any more questions" and it will store the input in variable anyQuestions if this variable has been declared.

The %s is a String place holder for the variable listed After the following comma. The %n is a new line character, (kind of like hitting enter after typing a sentence).

If "no" is input by the user then it will go into variable anyQuestions. The only way for the program to respond to an input of "no" is to run a conditional statement to test if anyQuestions is equal to no.

Like this:

if(anyQuestions.equalsIgnoreCase("No"){

  //run this code

}
else{

  // then run this code instead

}