Write a Guess My Number Game program The program generates a
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\");
}
}
}

