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
Assume you have the following array: int[] values = new int[15]; What will happen if the following code is executed? int[15] = 2
andrey2020 [161]

Answer:

You are creating an array "values" which stores 15 integers. And if we will write int[15]=20, we are not following correct syntax. Hence, nothing will happen, and it might however, throw an error that the syntax is wrong.

Explanation:

int[15]=20 is a wrong syntax, and hence nothing will happen or computer might throw an error like that not correct syntax is being used. And if we write values[15]=20,  It will certainly throw ArrayIndexOutOfBoundsException as the maximum length is fixed to 15 and values[14] and not more than that.  

3 0
4 years ago
Convert 10101010 into decimal number system​
geniusboy [140]

Answer:

170 hope this helps you with

3 0
3 years ago
Read 2 more answers
Which of the following is not a language commonly used for web programming? PHP Assembly Java Python
Nataly [62]

Answer:

If you’re a software developer, then you probably—every now and then—feel overwhelmed by the super-fast pace at which our industry evolves, and that’s fine. I certainly feel that way sometimes, especially when trying to keep up with the latest trends.

But it’s possible to be well-informed about what’s going on out there, and use that information to your advantage, by being economical about your learning.

Sure, there are lots of programming languages. Sure, new ones keep being created every week—and don’t even get me started on JavaScript frameworks.

Do you need to learn all of them? Of course not.

First, learn about a number of the most popular programming languages. Then, narrow that list down, by picking the ones that make the most sense for your current knowledge level, employment status and other criteria that might make sense for your scenario.

For instance, if you intend to learn the functional paradigm, then pick a functional language from the list. Rinse and repeat.

That’s what this post is about. We’ve done the legwork for you, compiling a list of five of the most popular programming languages for 2019. Now you only have to read it and put it to good use.

(Hope this helped if not i am so so sorry hope you have a blessed day goodbye)

7 0
3 years ago
Read 2 more answers
What are the similarities and differences between the sample résumés, in terms of content and formatting?
poizon [28]

Answer:

in general terms a sample resume would have less on it with more of a general overview,while a full resume has the whole 9 yards  

6 0
4 years ago
Can Anyone Send Me The Correct Code To This ?<br><br> ITS JAVA
scoundrel [369]

Answer:

whta u mean by code?

Explanation:

8 0
3 years ago
Other questions:
  • A software program that allows a programmer to type in code. modern versions usually make it easy to format the code on the scre
    13·1 answer
  • Ponce is the largest city on which coast of Puerto Rico
    13·1 answer
  • I have six nuts and six bolts. Exactly one nut goes with each bolt. The nuts are all different sizes, but it’s hard to compare t
    12·1 answer
  • Adding software to a digital device, or adding hardware components to a digital device is called _____ .
    13·1 answer
  • Which of the following terms is computed as cash provided by operations less capital expenditures and cash dividends? A : adjust
    5·1 answer
  • Functions can accept any number of arguments.
    8·1 answer
  • Write an application program in C++ to implement a class Fibonacci to print Fibonacci series upto N using member function series
    9·1 answer
  • Convert the following denary numbers into binary using 8-bit register:
    6·1 answer
  • Question 1 of 10 Which type of information systems personnel are involved in organizing information so that users can access it
    7·1 answer
  • There are three groups of people, people who like Apples, Bananas, and Cherries. 19 people like Apples. 23 people like Bananas.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!