Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following code segment is part of a guessing game. Complete the TWO while po

ID: 3625206 • Letter: T

Question

The following code segment is part of a guessing game. Complete the TWO while portions of the do-while loops so that the user can 1) keep guessing until he gets the correct answer, and 2) is asked to enter a guess until he enters a value between 1 and 10.

int correctAnswer = 3;
int theGuess;

Scanner keyboard = new Scanner(System.in);

do
{
do

{
System.out.print("enter a number from 1 to 10: ");
} while (ENTER ANSWER 1 HERE )
if (theGuess == correctAnswer)
{
System.out.println("Correct");
}
else
{
System.out.println("sorry wrong answer");
}
} while (ENTER ANSWER 2 HERE)


An example run of the code would look like this
enter a number between 1 and 10: 5
sorry wrong answer
enter a number between 1 and 10: 12
enter a number between 1 and 10: 0
enter a number between 1 and 10: 3
Correct

Explanation / Answer

ANSWER 1

Set theGuess to the entered value, and check that it's between 1 and 10.

((theGuess = kb.nextInt()) < 1 || theGuess > 10)

ANSWER 2

Loop while the guess is not correct.

(theGuess != correctAnswer)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote