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
alexandr1967 [171]
3 years ago
6

Write a program that generates a random number and asks the user to guess what the number is. If the user's guess is higher than

the random number, the program should display "Too high, try again." if the user's guess is lower than the random number, the program should display "Too low, try again." The program should use a loop that repeats until the user correctly guesses the random number. You shall also keep a count of the number of guesses that the user makes. When the user correctly guesses the random number, the program should display the number of guesses. Now add another loop to ask the user if he or she wishes to play the guessing game again. If so, the loop should repeat, otherwise it should terminate.So far, this is what my program looks like, any help or suggestions would be greatly appreciated!// This program generates a random number and askes the user to guess what the number isimport java.util.Scanner;import java.util.Random;public class GuessingGame{public static void main(String [] args){//Create a scanner object to read from the keyboardScanner kb = new Scanner(System.in);//Create a random objectRandom rand = new Random();//Identifier declarationsint num = rand.nextInt(100) + 1;int guess = 0;int count = 0;int guesses = 0;do{System.out.println("Guess what number I have (1-100)? ");guess = kb.nextInt();guesses ++;if(num > guess) {System.out.println("Too high, try again.");} else if(num < guess) {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");}}while(guess!=num);}}
Computers and Technology
1 answer:
JulsSmile [24]3 years ago
3 0

Answer:

import java.util.Scanner;

import java.util.Random;

public class Guesser {

// Create a scanner object to read from the keyboard

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

// Create a random object

static Random rand = new Random();

public static void main(String[] args) {

 playGame();

}

public static void playGame() {

 // Identifier declarations

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

 int guess = 0;

 int count = 0;

 int guesses = 0;

 do {

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

  guess = kb.nextInt();

  guesses++;

  if (num > guess) {

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

  } else if (num < guess) {

   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

   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 {

    break;

   }

  }

 } while (guess != num);

}

}

Explanation:

There are a few things you need to do.

1. Make the creation of the objects of the Scanner and Random classes global. In other words, make them global variables.

2. Create a method, say playGame(), that is called, recursively, every time the user needs to play or replay the game.  

The source code file (Guesser.java) has been attached to this answer.

Note: You might want to consider also reducing the range of guess. I'd rather you reduced it to 1-10 rather than what you have. This will make the game much of a fun. With the range you currently have, the user might never guess right.

Hope it helps!

You might be interested in
Coding with Loops Worksheet
BartSMP [9]

Answer:

if you give me brainliest i will answer your question

Explan

4 0
3 years ago
Read 2 more answers
Luminaires for fixed lighting installed in Class II, Division 2 locations shall be protected from physical damage by a suitable
ser-zykov [4K]

Given what we know, the protection for fixed lightings like the ones described in the question is by way of a guard or by location.

<h3>Why is protection necessary?</h3>

Luminaires, as with all lighting solutions, can be dangerous if proper safety precautions are not taken. The precautions, in this case, include a safe installation location or the use of a guard to prevent damage to the lighting and subsequently to any nearby occupants of the location.

Therefore, we can confirm that Luminaires for fixed lighting installed in Class II, Division 2 locations shall be protected from physical damage by a suitable guard or by a safe location.

To learn more about Electrical safety visit:

brainly.com/question/14144270?referrer=searchResults

4 0
3 years ago
A database program helps to ____________. A. Organize, send, and retrieve e-mails quickly b. Deliver elegant presentations to a
jarptica [38.1K]

Answer: D

Explanation: D. A database is software which is designed to store massive amounts of data and organize them in such a way that information can easily be worked on (added, deleted, moved, etc...).

7 0
3 years ago
The _____ model of a systems development life cycle provides for progressive development of operational software, with each rele
dimaraw [331]

Answer:

B. Incremental Build life cycle

Explanation:

RAD life cycle: RAD model of software development life cycle is all about coming up with prototype as soon as possible. It describes a method of software development which heavily emphasizes rapid prototyping and iterative delivery.

Incremental build life cycle: In incremental build model of software development life cycle, each release of the software have added capabilities. The development is finished when the user is okay with the present features. Focus is put on creating a working prototype first and adding features in the development life cycle. This is the correct option.

Waterfall life cycle: In a waterfall model, each phase must be completed fully before the next phase can begin and this usually takes time before a working software can be released. There is also no overlapping in the phases.

Spiral life cycle: Spiral Model can be pretty costly to use and doesn't work well for small projects. It's a risk-driven model which means that the overall success of a project highly depends on the risks analysis phase.

4 0
3 years ago
Read 2 more answers
Items on a storage medium remain intact even when power is removed from the computer. what term describes this characteristic?
stellarik [79]
Non-volatile, persistent or permanent storage are terms used for this.
7 0
3 years ago
Other questions:
  • Charles Lyell believed that:
    7·1 answer
  • Describe your dreams include lifestyle,job,house,friends
    5·2 answers
  • In which of the following situations will a macro make your work more efficient?
    12·1 answer
  • Help me
    14·1 answer
  • You work part time at a computer repair store. You are in the process of building a new computer system. The customer wants a mo
    6·1 answer
  • What type of device is the keyboard?<br><br> Output<br> Input<br> Monitoring<br> Software
    7·2 answers
  • 1 : What format would you apply to give an entry the appearance of 12-Mar-2014
    8·1 answer
  • चार फरक छुट्याउनुहोस् ।) 3. Write down any four provisions of cyber ethic साइबर नैतिकताका कुनै चार प्रावधानहरु लेख्नुहोस् ।​
    11·1 answer
  • PLEASE ANSWER
    6·1 answer
  • True or false. A plug-in card is always required to support pc sound
    11·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!