Write a “Higher / Lower” game where the user tries to guess a random number betw
ID: 3880189 • Letter: W
Question
Write a “Higher / Lower” game where the user tries to guess a random number between 1 and 100. Give the user hints as they guess as to whether the correct number is higher or lower than their guess. Keep track of the number of valid guesses. If the user enters a number that is outside the range of numbers, prompt the user to enter a valid number. When the user correctly guesses the number, the user wins, and the game ends. Display the number of tries it took to guess the correct number. Part 1: Using the software of your choice (some examples include Visio, Word, Power Point, online flowchart software, etc.) Create a flowchart diagram, showing the flow of logic for the program outlined above. Here is an example of what a flowchart looks like: Part 1: Working from the flowchart you created, now write the code for the program. Name your file Operation3. A sample session of how the program should interact is provided below. Sample Session: Welcome to the Higher / Lower game! Try to guess the number between 1 and 100. Enter your guess: -5 Sorry, the guess needs to be a number between 1 and 100. Please try again: -10 Sorry, the guess needs to be a number between 1 and 100. Please try again: 50 The number is higher. Enter your guess: 75 The number is lower. Enter your guess: 63 The number was 63! You guessed correctly! It took you 3 tries.
Explanation / Answer
GuessingGame.java
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
//Declaring variables
int secret, guess, no_of_guesses = 1;
String str = "";
//Creating the Random class object
Random generator = new Random();
System.out.println("Welcome to the Number Guessing Game");
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
//generating the random number
secret = generator.nextInt(100) + 1;
while (true) {
System.out.print("Enter your guess:");
guess = sc.nextInt();
if (guess < 1 || guess > 100) {
System.out.println("Sorry, the guess needs to be a number between 1 and 100.Please try again");
continue;
} else {
/* This while loop continues to execute
* until the user guesses the number
*/
while (guess != secret) {
if (guess > secret) {
System.out.println("The Number is higher.");
no_of_guesses++;
} else if (guess < secret) {
System.out.println("The Number is lower.");
no_of_guesses++;
}
System.out.print("Enter your guess:");
guess = sc.nextInt();
}
System.out.println("The Number was "+secret+"!");
System.out.println("You guessed correctly!");
//Displaying the no of chances user took to guess
System.out.print(" It took you :" + no_of_guesses+" tries");
}
break;
}
}
}
_________________
Output:
Welcome to the Number Guessing Game
Enter your guess:110
Sorry, the guess needs to be a number between 1 and 100.Please try again
Enter your guess:-45
Sorry, the guess needs to be a number between 1 and 100.Please try again
Enter your guess:50
The Number is lower.
Enter your guess:80
The Number is higher.
Enter your guess:65
The Number is lower.
Enter your guess:70
The Number was 70!
You guessed correctly!
It took you :4 tries
_______________Could you plz rate me well.Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.