Write a Guess My Number Game program The program generates a

Write a \"Guess My Number Game\" program. The program generates a random integer in a specified range and the user (the player) has to guess the number. The program allows the user to play as many times as he/she would like, at the conclusion of each game, the program asks whether the player wants to play again. The basic algorithm is as follows. The program starts by printing instructions on the screen. For every game: the program generates a new random integer in the range from MIN to MAX. Treat MIN and MAX like constants; start by initializing them to 1 and 100 loop to prompt the player for a guess until the player correctly guesses the integer for each guess, the program prints whether the player\'s guess was too low, too high, or correct. At the conclusion (when the integer has been guessed): print the total number of guesses for that game. print a message regarding how well the player did in that game (e.g., the Player took way too long to guess the number, the player was awesome etc.) to do this, you will have to decide on ranges for your messages and give a rationale for your decision in a comment in a comment in the program. After all games have been played, print a summary showing the average number of guesses.

Solution

package com.chegg.ques;

import java.util.Random;
import java.util.Scanner;
import java.lang.System;

public class Game {

public static void main(String[] args){

// instruction for the game

System.out.println(\"Hello and welcome to my number guessing game.\");

System.out.println(\"Pick a number: \");

Scanner inputnum = new Scanner(System.in); //This number will be the max number the player has to guess too.

// initializing MIN and MAX value to 1 and 100 respectively   

int MIN=1;

int MAX=100;

// generating random number in range of 1-100

Random rand = new Random();
int number = rand.nextInt(MAX);
int tries = 0; //Will increase depending on how many tires it takes
Scanner input = new Scanner(System.in);
int guess;
boolean win = false;

while (win == false){ //This while loop false the code with in it repeat until win === true

System.out.println(\"Guess a number between 1 and \"+ 100 +\": \");
guess = input.nextInt();
  
tries++; //Increasing the number set in the variable tries by 1

if (guess == number){
win = true; //Since the number is correct win == true then ending the loop
//First thing the guess is compared too
}

else if(guess < number){
System.out.println(\"Number is too low\");
//2nd thing guess is compared too.
}

else if(guess > number){
System.out.println(\"Number is too high\");
//3rd thing guess is compared too.
}

}

System.out.println(\"You win!\");
System.out.println(\"It took you \"+ tries + \" tries.\");

if(tries<=25){
   System.out.println(\"Excellent !!!\");
}else if(tries>=26 && tries<=50){
   System.out.println(\"Well Played !!!\");
}else if(tries>=51 && tries<=75){
   System.out.println(\"Good !!!\");
}else{
   System.out.println(\"keep practicing !!!\");
}

System.out.println(\"Do u want to play again.....(Y|N)\");
   char ch=input.next().charAt(0);
   if(ch==\'Y\' || ch==\'y\'){
       main(args);
   }else if(ch==\'N\' || ch==\'n\'){
       System.out.println(\"Thank you...!!!\");
   }
   else{
       System.out.println(\"Wrong input\");
   }

}
}

 Write a \
 Write a \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site