Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using eclipse java You will be writing a \"guess my number\" game in Java. Essen

ID: 3680659 • Letter: U

Question

using eclipse java You will be writing a "guess my number" game in Java. Essentially, this program should: Generate a random integer number between 1 and 100 inclusively). This will be the computer's "secret" number. Repeatedly do the following until the user guesses the computer's number, the user makes 10 unsuccessful guesses, or the user decides to quit: prompt the user to enter their next (integer) guess if the user's guess was more than the computer's "secret" number, print out a message saying the user's guess was too high. if the user's guess was less than the computer's "secret" number, print out a message saying the user's guess was too low. prompts the user to enter "Quit" of they would like to stop playing the game (and any other word if they want to keep going). If the user stopped guessing because they chose the computer's "secret" number, print out a message indicating such. If the user stopped guessing because they entered the string "Quit", then print out a message calling them a "quitter" and indicate what the "secret" number was. If the user stopped guessing because they ran out of guesses, print out a message telling them they ran out of guesses and indicate what the "secret" number was.

Explanation / Answer

Hi, Please find the below program for above reqirement.

import java.util.Random;

public class GuessNumberGame {

   /**
   * @param args
   */
   public static void main(String[] args) throws Exception {
       // TODO Auto-generated method stub
       java.util.Scanner in = new java.util.Scanner(System.in);
       char c;
       Random r = new Random();
       int Low = 1;
       int High = 100;
       int secret = r.nextInt(High-Low) + Low;
final int NUMBER_USER_CHOICE = 10;
       for(int i=0; i<NUMBER_USER_CHOICE; i++){
       System.out.println("Please Enter user choice ["+(i+1)+"] time ...");
       int user_choice = in.nextInt();
       if(secret < user_choice){
           System.out.println("The User guess was too high");
       }
       else if(secret > user_choice){
           System.out.println("The User guess was too low");
       }
       else if(secret == user_choice){
           System.out.println("The User guess was correct. User choice ["+user_choice+"] and System secret number ["+secret+"] are same.");
           break;
       }
       if(i == NUMBER_USER_CHOICE - 1){
           System.out.println("You have exceeded 10 guesses. The system secret number is ["+secret+"].");
       }
       else{
       System.out.println("Do you want to guess the secret number again? If no, Press Q for Quit or Press any character to continue...");
       c = in.next().charAt(0);
       if(c == 'Q' || c == 'q'){
           System.out.println("You have successfully stopped playing the Game. System secret number is ["+secret+"].");
           break;
       }
       }
       }
   }

}

Output:

Please Enter user choice [1] time ...
21
The User guess was too low
Do you want to guess the secret number again?
If no, Press Q for Quit or Press any character to continue...
a
Please Enter user choice [2] time ...
34
The User guess was too low
Do you want to guess the secret number again?
If no, Press Q for Quit or Press any character to continue...
f
Please Enter user choice [3] time ...
45
The User guess was too low
Do you want to guess the secret number again?
If no, Press Q for Quit or Press any character to continue...
f
Please Enter user choice [4] time ...
67
The User guess was too low
Do you want to guess the secret number again?
If no, Press Q for Quit or Press any character to continue...
h
Please Enter user choice [5] time ...
89
The User guess was too low
Do you want to guess the secret number again?
If no, Press Q for Quit or Press any character to continue...
b
Please Enter user choice [6] time ...
96
The User guess was too low
Do you want to guess the secret number again?
If no, Press Q for Quit or Press any character to continue...
j
Please Enter user choice [7] time ...
97
The User guess was too low
Do you want to guess the secret number again?
If no, Press Q for Quit or Press any character to continue...
j
Please Enter user choice [8] time ...
98
The User guess was correct. User choice [98] and System secret number [98] are same.