1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Leona [35]
3 years ago
13

Design and implement an application that plays the Hi-Lo guessing game with numbers. The program should pick a random number bet

ween 1 and 100 (inclusive), then repeatedly prompt the user to guess the number. On each guess, report to the user that he or she is correct or that the guess is high or low. Continue accepting guesses until the user guesses correctly or chooses to quit. Use a sentinel value to determine whether the user wants to quit. Count the number of guesses and report that value when the user guesses correctly. At the end of each game (by quitting or a correct guess), prompt to determine whether the user wants to play again. Continue playing games until the user chooses to stop.
Computers and Technology
1 answer:
AnnyKZ [126]3 years ago
7 0

Answer:

import java.util.Scanner;

import java.util.Random;

public class Hi_Lo {

// Create a scanner object to read from the keyboard

static Scanner kb = new Scanner(System.in);

// Create a Random object to be used to generate random numbers

static Random rand = new Random();

// This is the main method

public static void main(String[] args) {

 // Call the playGame() method

 playGame();

}

public static void playGame() {

 // Generate a random number between 1 and 100 both inclusive

 // Store the value in an int variable, num

 int num = rand.nextInt(100) + 1;

 // Create and initialize a variable to hold the user's guess

 int guess = 0;

 // Create and initialize a variable to hold the number of guesses

 int guesses = 0;

 // Create an infinite do while loop that keeps looping until a certain condition

 // is reached.

 do {

  // Prompt the user to enter a number between 1-100

  System.out.println("Guess what number I have (1-100)? ");

  // Store the user input in the guess variable

  guess = kb.nextInt();

  // Increment guess by one

  guesses++;

  // Check if the user's guess (guess) is greater than, less than or equal to the

  // generated random number (num).

  // If it is greater or lower, ask the user to try again.

  // If they are equal, display how many times they guessed and ask if they want

  // to play again.

  if (guess > num) {

   System.out.println("Too high, try again.");

  } else if (guess < num) {

   System.out.println("Too low, try again.");

  } else {

   System.out.println("You're right, the number is " + num);

   System.out.println("You guessed " + guesses + " times");

   // Ask the user if they want to play again

   // If yes, recursively call the playGame method again

   // Otherwise, break out of the loop.

   // Make sure the user's input is treated as case insensitive by using the

   // equalsIgnoreCase method.

   // The user can type yes, no, y or n. Case does not matter.

   System.out.println("Do you want to play again? (Y or N)");

   String answer = kb.next();

   if (answer.equalsIgnoreCase("Yes") || answer.equalsIgnoreCase("Y")) {

    playGame();

   } else {

    System.out.println("Game aborted. Thanks for your time");

    break;

   }

  }

 } while (true);

}

}

Answer has also been attached to this response as a source code file. Please download the file for more readability.

Explanation:

The code contains comments explaining every segment of the code. Kindly download the file and go through the comments in the code.

Hope this helps!

Download java
You might be interested in
What mode is generally used when delivering a presentation to an audience?
dybincka [34]

Answer:

delivery mode

Explanation:

there it is

7 0
3 years ago
Read 2 more answers
Which of the following is a hand-drawn animation that takes a large amount of time to complete?
elixir [45]

Answer:

Where are the choices?

Thanks for adding the choices at the comment secion

  1. 2D animation
  2. 3D animation
  3. Stop Motion  
  4. Celluloid animation

A- Celluloid Animation

4 0
3 years ago
Which font style is said to be slightly above the other line of text?
Keith_Richards [23]
I think the answer would be "superscript"
8 0
3 years ago
Read 2 more answers
Using the find and replace tool would be most appropriate when
mafiozo [28]

A. Changing the company name when the same letter is sent to different companies

The find and replace tool is meant to help replace all instances of a certain piece of text with a different piece of text.

For example, if a letter was sent to Company A, the find and replace tool could change every time the letter says “Company A” and make it say “Company B” instead so the same letter could be sent to Company B.

3 0
3 years ago
Read 2 more answers
Microsoft word 365 allows cells to be split into how many columns
Bogdan [553]

Answer:

Microsoft word 365 allows two columns

8 0
3 years ago
Read 2 more answers
Other questions:
  • Which of these is a preferred method for
    14·2 answers
  • 7.14 LAB: Word frequencies Write a program that reads a list of words. Then, the program outputs those words and their frequenci
    10·1 answer
  • A user is experiencing slow performance with their computer. A technician suspects the computer has a virus and runs antivirus s
    12·1 answer
  • When a JSP page is compiled, what is it turned into?
    11·1 answer
  • FRee points just put something
    14·2 answers
  • Which iteration must have an expression that has a true or false value?
    11·2 answers
  • What do you understand by the term polysome?​
    14·1 answer
  • What do you think Amazon should fix?
    13·2 answers
  • What is one of the advantages of using an algorithm to solve a puzzle like Kriss-Kross or Sudoku?
    13·1 answer
  • What are the characteristics of calendar sharing options in Outlook 2016? Check all that apply.
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!