Question

Write a Java application program that plays a number guessing game with the user. In the...

Write a Java application program that plays a number guessing game with the user.

In the starter code that you are given to help you begin the project, you will find the following lines of code:

Random generator = args.length == 0 ? new Random() :
                   new Random(Integer.parseInt(args[0]));

int secret = generator.nextInt(100);

You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program will fail all of the tests.

Your program should begin by choosing a random number between 0 and 99 (inclusive). After executing the above statements, the variable secret will be holding a randomly generated number from 0 to 99.

Next, your program should ask the user to try to guess what number was chosen.

If the user does not guess the number correctly, you should tell the user that the secret number is higher or lower than the one they guessed. At this point you should allow them to guess again.

Repeat this process until the user guesses the number correctly.

Whenever the user does correctly guess the number, you should stop the program and print to the screen the number of tries that it took the user to guess the number. Make sure to use correct grammar when printing your final message to the user (for example "1 guess" or "2 guesses" -- make sure your program doesn't say "1 guesses" or "2 guess").

The following is an example of what your MIGHT see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user's values are shown below in italics (note that the secret number in this example is 32):

I am thinking of a number from 0-99.
Try to guess my number: 50
My number is lower. Guess again: 25
My number is higher. Guess again: 33
My number is lower. Guess again: 31
My number is higher. Guess again: 32

Congratulations! It only took you 5 guesses!

Notes:

While testing your program, (and ONLY while testing your program), you can simply print the secret number to the screen at the start of the program. By doing this, you can properly test your program by knowing the correct answer.

For example after the required lines:

Random generator = args.length == 0 ? new Random() :
                   new Random(Integer.parseInt(args[0]));

int secret = generator.nextInt(100);

Add a print statement:

System.out.println(secret);

The reason for doing this is so you will know the answer to the problem and therefore will know whether or not the program is correctly telling you "higher" or "lower" when you are making your guesses. Obviously, once you know the program works correctly, you will want to remove this print statement from your program, otherwise the user will always know the answer when playing and therefore it isn't much of a game.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

GuessingGame.java

import java.util.Random;

import java.util.Scanner;

public class GuessingGame {

public static void main(String[] args) {

Random generator = args.length == 0 ? new Random() :

new Random(Integer.parseInt(args[0]));

int secret = generator.nextInt(100);

Scanner scan = new Scanner(System.in);

System.out.println("I am thinking of a number from 0-99.");

System.out.println("Try to guess my number: ");

int n = scan.nextInt();

int count = 0;

while(n!=secret){

count++;

if(n<secret) {

System.out.println("My number is lower. Guess again: ");

} else {

System.out.println("My number is higher. Guess again: ");

}

n = scan.nextInt();

}

System.out.println("Congratulations! It only took you "+count+" guesses!");

}

}

Output:

I am thinking of a number from 0-99.
Try to guess my number:
66
My number is higher. Guess again:
44
My number is higher. Guess again:
22
My number is higher. Guess again:
11
My number is lower. Guess again:
16
My number is lower. Guess again:
19
My number is higher. Guess again:
17
My number is lower. Guess again:
18
Congratulations! It only took you 7 guesses!

Add a comment
Know the answer?
Add Answer to:
Write a Java application program that plays a number guessing game with the user. In the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it)...

    Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it) Program should be able to guess correctly in 7 tries or lower. Initialize a variable that represents the lowest possible number to 0 (what type should this be?) Initialize a variable that represent the highest possible number to 100 (what type should this be?) Initialize a Boolean variable that represents if we’ve achieved the correct guess to false Initialize a variable in which to...

  • Write a program that allows a user to play a guessing game. Pick a random number...

    Write a program that allows a user to play a guessing game. Pick a random number in between 1 and 100, and then prompt the user for a guess. For their first guess, if it’s not correct, respond to the user that their guess was “hot.” For all subsequent guesses, respond that the user was “hot”if their new guess is strictly closer to the secret number than their old guess and respond with“cold”, otherwise. Continue getting guesses from the user...

  • Specification Write a program that allows the user to play number guessing games. Playing a Guessing...

    Specification Write a program that allows the user to play number guessing games. Playing a Guessing Game Use rand() function from the Standard C Library to generate a random number between 1 and 100 (inclusive). Prompt the user to enter a guess. Loop until the user guesses the random number or enters a sentinel value (-1) to give up. Print an error message if the user enters a number that is not between 1 and 100. Print an error message...

  • Write a program to play "guess my number". The program should generate a random number between...

    Write a program to play "guess my number". The program should generate a random number between 0 and 100 and then prompt the user to guess the number. If the user guesses incorrectly the program gives the user a hint using the words 'higher' or 'lower' (as shown in the sample output). It prints a message 'Yes - it is' if the guessed number is same as the random number generated. ANSWER IN C LANGUAGE. ====================== Sample Input / Output:...

  • Write a JAVA program that plays a number guessing game with the user. A sample run...

    Write a JAVA program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in boldface in the sample run. Welcome to the game of Guess It! I will choose a number between 1 and 100. You will try to guess that number. If your guess wrong, I will tell you if you guessed too high or too low. You have 6 tries to get the number. OK, I am...

  • This program will implement a simple guessing game... Write a program that will generate a random...

    This program will implement a simple guessing game... Write a program that will generate a random number between 1 and 50 and then have the user guess the number. The program should tell the user whether they have guessed too high or too low and allow them to continue to guess until they get the number or enter a 0 to quit. When they guess the number it should tell them how many guesses it took. At the end, the...

  • Solve Question using While loops-MATLAB Write a program that generates a random number and asks the...

    Solve Question using While loops-MATLAB 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.

  • Using C programming REQUIREMENTS: This program is a letter guessing game where a simple Al opponent...

    Using C programming REQUIREMENTS: This program is a letter guessing game where a simple Al opponent generates a secret letter for the user to guess. The user must be able to take turns guessing the secret letter, with the Al responding to the user's guesses. After successfully guessing, the user must be allowed to play again. The Al must count how many turns the user has taken. Here is an example of a game in progress where the human user...

  • (c++) Write a program that generates a random number between 1 and 100 and asks the...

    (c++) Write a program that generates a random number between 1 and 100 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. Be sure that your program...

  • Hello! we are using Python to write this program. we are supposed to use loops in...

    Hello! we are using Python to write this program. we are supposed to use loops in this assignment. I would greatly appreciate the help! Thank you! Write a program that allows the user to play a guessing game. The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: we would have the program select a random number as the "secret number". However, for the purpose of testing...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT