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

Write a Java program that implements a simple number guessing game with multiple

ID: 3567136 • Letter: W

Question

Write a Java program that implements a simple number guessing game with multiple questions / answers. The Java class name must be GameXXXXX where XXXXX is your student ID. For each game, the program generates a random number between 1 and 10. User enters an answer from 1 and 10. If the user input number matches the generated number, then print a message to inform users that he/she has a correct guess. If the guess is not correct, allow the user to have two more chances to guess the correct number. At any time, if users enter 0, then the program should display the session summary and exits. The program should keep track of the wins and losses and print the counts when user chooses to exit by entering 0. The program should generate a new random number only after user enters the correct guess or after user has tried 3 times and did not have the right guess. Do not ask user for a yes/no confirmation after each game because the 0 input value will serve as the sentinel to stop the continuous game.

Here is a sample run using command line.

At the program start, it shows:

Explanation / Answer

import java.util.Random;
import java.util.Scanner;


public class GameX {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       int Game[]=new int[100];
       int Secret[]=new int[100];
       int Trials[]=new int[100];
       int temp=1;
       System.out.println("Welcome to the number guessing game!!");
       int temp1=0,i,j=0;
       while(temp1!=1){
       Random r=new Random();
       int number=r.nextInt(9)+1;
       Scanner s=new Scanner(System.in);
       for(i=0;i<3;i++){
           if(i==0){
       System.out.print("Enter a number from 1 t 10 Enter -1 exit:");
           }
           else{
               System.out.print("Not correct, try again:");
           }
       int inp=s.nextInt();
if(inp==number){
   System.out.println("Congratulatios,Correct!Let's start a new game.");
   temp=0;
   break;
}
else{
   if(inp==-1){
       temp1=1;
break;
   }
  
}

       }
       Game[j]=j+1;
       Secret[j]=number;
      
       Trials[j]=i;
       j++;
       }
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote