Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help correct all the code below in Java: import java.util.Random; import java.util.Scanner; public class HiLo { public static void main(String[] args) { Scanner sc

Please help correct all the code below in Java:

import java.util.Random; import java.util.Scanner;

public class HiLo {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

Random rand = new Random();

char choice;

int guess, num;

// Loop to run the program till user quits

do {

// Generate a random number between 1 and 100

num = rand.nextInt(100) + 1;

// Initialize number of guesses to 0

int count = 0; do { System.out.print("Enter your guess: "); guess = sc.nextInt(); sc.nextLine();

if (guess >= 1 && guess <= 100) { ++count;

if (guess == num) { System.out.println("Guess is Correct! You took " + count + " turns."); break; } else if (guess < num) { System.out.println("Guess is low!"); } else { System.out.println("Guess is high!"); } } else { System.out.println("You have entered value out of range. Please enter between 1 - 100."); }

System.out.print("Do you want to go back? (y/n) "); choice = sc.nextLine().charAt(0); } while (choice != 'y' && choice != 'Y');

System.out.print("Do you want to play again? (y/n) "); choice = sc.nextLine().charAt(0); } while (choice == 'y' || choice == 'Y');

sc.close(); } }

The question asked is:

Design and implement an application that plays the Hi-Lo guessing game with numbers. The program should pick a random number between 1 and 100 (inclusive), then repeatedly prompt the user to guess the number. On each guess, report to the user that they are correct or that the guess is high or low. Continue accepting guesses until the user guesses correctly or chooses to quit. Count the number of guesses and report that value when the user guesses correctly. If a guess is out of the range 1 to 100, an appropriate message should be displayed to the user and the guess not be counted. At the end of each game (by quitting or a correct guess), prompt to determine whether the user wants to play again. Continue playing another game until the user chooses to stop.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions