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.
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!
Write a Java application program that plays a number guessing game with the user. In the...
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 prompts the user for an integer that the player (maybe the user, maybe someone else) will try to guess. If the player's guess is higher than the target number, the program should display "too high" If the user's guess is lower than the target number, the program should display "too low" The program should use a loop that repeats until the user correctly guesses the number. Then the program should print how many guesses it took....
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...
Required in JAVA language Write a program that enables a user to play number guessing games. The following is a nutshell description of a number guessing game. + a random number is generated + loop prompting the user to enter guesses until the user guesses the number or hits the maximum number of allowed guesses or enters the "quit" sentinel value The rest of this specification documents number guessing games in more detail. The documentation uses manifest constants that can...
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 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 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...
Java Guessing Game Class The class will generate a random number of 1 to 15, and then check to see if the user guessed the number correctly. If the number is incorrect, the user should have the chance to guess again, until they guess the right number or they guess 10 times. The class method should keep track of the number of guesses the user has had, and return this value. The class should have one constructors, a default and...
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 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.