3.666666 You MUST add comments all throughout your code to explain what your cod
ID: 3863921 • Letter: 3
Question
3.666666 You MUST add comments all throughout your code to explain what your code is trying to do.
Pay close attention to the directions!! Make absolutely certain that you are doing exactly what the assignment asks you to do. If you don't understand the problem, make sure to ask your instructor or an assistant.
Declare all of your variables at the beginning of the program. Add blank lines between sections of code to space out your program more and make the sections easier to see.
part 3
Create a new Java class inside your project folder.
The name of the class should be: NumberGame
Write a java application program that plays a number guessing game with the user.
Your program should begin by choosing a random number between 0 and 99 (inclusive).
YOU MUST include the following statements to do this. Insert them into your program EXACTLY as shown below and do not change anything about them! You will not received credit for this problem if the following three lines are not found in your program.
int secret;
Random generator = new Random();
secret = generator.nextInt(100);
After executing the above statements, the variable secret will be holding a randomly generated number from 0 to 99. If you need to get another randomly generated number, you just simply repeat the last line again. Do not repeat all three lines.
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").
Notes:
While testing your program, you can simply set secret to a known value, so that you can properly test your program by knowing the correct answer.
For example:
int secret;
Random generator = new Random();
secret = generator.nextInt(100);
secret = 54;
Here, I have overwritten the random number, of which I don't know the value, with a number - 54 - which I obviously do know the value. The purpose is so I will know the answer to the problem and therefore will know whether or not the program is correctly telling me "higher" or "lower" when I am making my guesses. Obviously, once you know the program works correctly, you will want to take the secret=54 line out so the answer will be random each time the program executes.
Explanation / Answer
NumberGame.java
import java.util.Random;
import java.util.Scanner;
public class NumberGame {
public static void main(String[] args) {
//Declaring variables
int secret, guess, no_of_guesses = 0;
//Creating the Random class object
Random generator = new Random();
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
//This loop will continue to execute until the user enters either 'Y' or 'y'
while (true) {
//generating the random number
secret = generator.nextInt(100);
//this loop continues to execute until the user correctly guesses the number;
while (true) {
//Getting the user guess entered by the user
System.out.print("Guess the number (Between 0-99):");
guess = sc.nextInt();
if (guess == secret) {
System.out.println("** Correct **");
if (no_of_guesses == 1)
System.out.println("No of Guesses :"+no_of_guesses + " guess");
else
System.out.println("No of Guesses :"+no_of_guesses + " guesses");
break;
} else {
if (guess > secret) {
System.out.println("** Higher **");
no_of_guesses++;
continue;
} else {
System.out.println("** Lower **");
no_of_guesses++;
continue;
}
}
}
// Getting the character from the user 'Y' or 'y' or 'N' or 'n'
System.out.print("Do you want to Play Again(Y/N) ::");
char ch = sc.next(".").charAt(0);
if (ch == 'Y' || ch == 'y')
continue;
else {
System.out.println(":: Program Exit ::");
break;
}
}
}
}
__________________
output:
Guess the number (Between 0-99):50
** Higher **
Guess the number (Between 0-99):25
** Lower **
Guess the number (Between 0-99):30
** Lower **
Guess the number (Between 0-99):40
** Lower **
Guess the number (Between 0-99):45
** Lower **
Guess the number (Between 0-99):48
** Lower **
Guess the number (Between 0-99):49
** Correct **
No of Guesses :6 guesses
Do you want to Play Again(Y/N) ::Y
Guess the number (Between 0-99):50
** Lower **
Guess the number (Between 0-99):80
** Lower **
Guess the number (Between 0-99):90
** Higher **
Guess the number (Between 0-99):85
** Correct **
No of Guesses :9 guesses
Do you want to Play Again(Y/N) ::N
:: Program Exit ::
__________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.