View the step-by-step solution to:

Question


Forming a team You may work with a partner on this

assignment.

Guessing Game In Java, you can generate a pseudorandom integer by calling the nextInt method on a java.util.Random object. For example, the following complete program prints 10 pseudorandom numbers between 0 and 99, inclusive:


  1. import java.util.Random;
  2. public class TenRandomNumbers {
  3. public static void main(String[] args) {
  4. // Make a Random object
  5. Random rand = new Random();
  6. int i = 0;
  7. while (i < 10) {
  8. System.out.print(rand.nextInt(100) + " ");
  9. i++;
  10. }
  11. }
  12. }


Each time you run the program it prints a different sequence of numbers.


The program's user is the player. The player is supposed to guess an integer in the range 1-100, inclusive. The program should generate a pseudorandom number in this range. The program's generated number is the correct answer, or secret answer. The program compares the player's guess to the correct number:


  • If the player's number is less than the correct number, then the program should print "Too low" and allow the player to guess again.
  • If the player's number is greater than the correct number, then the program should print "Too high" and allow the player to guess again.
  • If the player's number is the same as the correct number, the game is over.


Within a loop, the game continues until the player guesses the correct answer. When the game ends, the program prints the total number of guesses, counting the correct guess as one of the guesses. The player's goal is to guess the number with the fewest attempts.


The following shows a sample run of the program:


  1. Guess a value from 1 to 100: 50
  2. Too low, please try again
  3. Guess a value from 1 to 100: 75
  4. Too high, please try again
  5. Guess a value from 1 to 100: 64
  6. Too high, please try again
  7. Guess a value from 1 to 100: 55
  8. Too high, please try again
  9. Guess a value from 1 to 100: 52
  10. Too high, please try again
  11. Guess a value from 1 to 100: 51
  12. 51 is the correct answer
  13. It took you 6 tries to get the correct answer


Hint: While you are developing your program you can print the answer (secret number to guess) each time the program requests a new guess. That way you can better tell if your program is working properly; otherwise, it is difficult to determine if the program is giving the player the correct feedback. Be sure to disable this "feature" in your final version, or it will not be much of a game!

Top Answer

Source Code: import java.util.Random; import java.util.Scanner; public class RandomNumbers { public static void... View the full answer

Screenshot from 2020-02-11 20-57-46.png

~/Desktop/course/RandomNumbers.java - Sublime Text (UNREGISTERED)
File Edit Selection Find View Goto Tools Project Preferences
Help
RandomNumbers.java x
import java. util. Random;
WN
import java....

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question