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
Which one of the following items would you be most likely to keep in a database? A. Payroll records B. Address book C. Financial
True [87]
The item that you would most likely to keep in a database is a Payroll record. Payroll records are numbers and inputs/outputs of employees of a certain company. Numbers are easier to manipulate and easier to manage than statements, letters and addresses that are basically letters.
3 0
3 years ago
23. What is the value of 3 - (4 + 2 *3)/(8-2) + 7 * 2+4
Artist 52 [7]

Answer:

30 djjjjj*fjfktdyldydxpitdotdtiotdtod

8 0
3 years ago
According to Gitlin, during the 1950s and 60s television production costs began to exceed the licensing fees the networks paid i
wlad13 [49]

Answer:

syndication

Explanation:

According to Gitlin, during the 1950s and 60s television production costs began to exceed the licensing fees the networks paid in order to broadcast their programming. But the studios could make that money back by putting a show in syndication after it produced 100 episodes that could be programmed in re-runs. Syndication is the licensing or sale of a publication material by television stations.

5 0
3 years ago
What are the two different types of dns requests?
Fantom [35]

The two major types of DNS request are as follows;

  • Recursive Query
  • Iterative Query

<h3>What is a domain name system?</h3>

Domain name system is abbreviated as DNS.

A Domain Name System (DNS) converts domain names into IP addresses, which allow browsers to get to websites and other internet resources.

Basically,  DNS is a directory of names that match with numbers.

Computer uses IP addresses to communicate, therefore, the DNS convert domain names to IP address for communication.  

The two major types of DNS request are as follows;

  • Recursive Query - Query that demands a resolution
  • Iterative Query - Query that does not demand a resolution.

learn more on DNS here: brainly.com/question/14397200

#SPJ11

6 0
2 years ago
Read 2 more answers
Create a method called nicknames that passes an array as a parameter. Inside the method initialize it to hold 5 names of your ch
Gre4nikov [31]
<h2>Answer:</h2>

    //======METHOD DECLARATION=========//          

    //method name: nicknames                                          

    //method return type: void                                            

    //method parameter: an array reference                    

    public static void nicknames(String [] names){      

       //initialize the array with 5 random names              

       names = new String[] {"John", "Doe", "Brian", "Loveth", "Chris"};

       //using an enhanced for loop, print out the elements in the array

       for(String n: names){

           System.out.print(n + " ");

       }

       

    }

<h2>Explanation:</h2>

The program is written in Java. It contains comments explaining important parts of the code. Kindly go through these comments.

A few things to note.

i. Since the method does not return any value, its return type is <em>void</em>

ii. The method is made public so that it can be accessible anywhere in and out of the class the uses it.

iii. The method is made static since it will most probably be called in the static main method (at least for testing in this case)

iv. The method receives an array of type <em>String </em>as parameter since the names to be stored are of type <em>String</em>.

v. The format of initializing an array manually should follow as shown on line 7. The <em>new</em> keyword followed by the array type (String), followed by the square brackets ([]) are all important.

vi. An enhanced for loop (lines 9 - 11) is a shorthand way of writing a for loop. The format is as follows;

=> The keyword <em>for</em>

=> followed by an opening parenthesis

=> followed by the type of each of the elements in the array. Type String in this case.

=> followed by a variable name. This holds an element per cycle of the loop.

=> followed by a column

=> followed by the array

=> followed by the closing parenthesis.

=> followed by a pair of curly parentheses serving as a block containing the  code to be executed in every cycle of the loop. In this case, the array elements, each held in turn by variable n, will be printed followed by a space.

A complete code and sample output for testing purposes are shown as follows:

==================================================

public class Tester{

    //The main method

    public static void main(String []args){

       

       String [] names = new String[5];

       nicknames(names);

    }

   

  //The nicknames method

    public static void nicknames(String [] names){

       names = new String[] {"John", "Doe", "Brian", "Loveth", "Chris"};

       for(String n: names){

           System.out.print(n + " ");

       }

       

    }

}

==================================================

<h2>Output:</h2>

John Doe Brian Loveth Chris

<em>NB: To run this program, copy the complete code, paste in an IDE or editor and save as Tester.java</em>

6 0
2 years ago
Other questions:
  • ¿Cómo es la onda percibida por un osciloscopio cuando hablamos de sonido? ¿Qué parámetros podemos observar en ella?
    15·1 answer
  • What is the name of the port that you plug an ethernet network cable into?
    6·1 answer
  • A rang check that could be use to validate... Date and time
    13·1 answer
  • Write a function safeOpen() that takes one parameter, filename — a string giving the pathname of the file to be opened for readi
    15·1 answer
  • give the difference between functional and functional tools in the middle of to the circle give importance​
    10·1 answer
  • You decide to test an audio clip that you have inserted into your presentation, but before you do that you make sure that the co
    6·1 answer
  • Identify the correct characteristics of Python tuples. Check all that apply.
    9·1 answer
  • 4.7 Code Practice: Question 2
    7·1 answer
  • Join each of the follwing sentences using unless
    14·2 answers
  • PLEASE ANSWER! I NEED IN 30 MIN.
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!