the answer for this question is posted on chegg twice but it is wrong. please us
ID: 3865188 • Letter: T
Question
the answer for this question is posted on chegg twice but it is wrong. please use file type reader to find the existing file. thanks.
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class HiLo
{
public static void main (String[] args)
{
final int MAX = 10;
int answer, guess;
int numberOfTries = 0 ;
String again;
Scanner Keyboard = new Scanner(System.in);
do
{
System.out.print (" I'm thinking of a number between 0 and "
+ MAX + ". Guess what it is: ");
guess = Keyboard.nextInt();
Random generator = new Random();
answer = generator.nextInt(MAX) +1;
if (guess > 10)
{
System.out.println ("ERROR – Your guess is out of the range 0 to 10.");
}
if (guess < 0)
{
System.out.println ("ERROR – Your guess is out of the range 0 to 10.");
}
while (guess != answer ) {
if (guess > answer )
{
System.out.println ("You guessed too high! Try again:");
guess = Keyboard.nextInt();
}
if ( guess == answer)
{
numberOfTries += 1; // <--- This adds the final guess
System.out.println ("YOU WIN!");
System.out.println("It took you " + numberOfTries + " tries!") ;
System.out.println();
System.out.print( "Do you want to play again(Y/N)?");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.