Hello I am having trouble with modifying the following progr

Hello, I am having trouble with modifying the following program in Java:

// import a class for random numbers

import java.util.Random;

public class GuessGame

{

     // method to generate a random number

     public static int RandomNumber()

     {

           // local variable to hold a random number

           int rNum = 0;

           // a random object is created

           Random rand = newRandom();

           // a random number is generated

           rNum = rand.nextInt(20) + 1;

           // random number is returned to main()

           return rNum;

     }

     // the main() method

     public static void main(String args[])

     {

           // local variable to hold a random integer     

           int x = 0;

           // local variable is assigned a random number

           x = RandomNumber();

           //display the random number

           System.out.println(x);

     }

}

I have to use the following steps to modify the program above:

To modify the above program to include the project’s directives, first define a

void return type method named guessingGame(). Place the definition for this method outside of the main() method bit within the class definition. This method will be used to call the RandomNumber() method from Figure 1 .

Within the named guessingGame() method you can use these suggested steps.

•           Declare some local module level variables.

                intx=0,guess=0,response=1;

•           Open a dowhile() looping structure that executes its loop body as        long as the variable response equals 1 .

                                   

                        •           Assign local variable x to the call of the RandomNumber() method.

                       

                        •           Display this message to the user.

                \"Welcome: the guessing game. \ I have a number \"

                + \"from 1 \" +\"and 20.\"+\"Can you guess it?\"+

                \"Please type your first guess.\"

                       

                        •           Input the users guess .

                                               

                        •           Open an inner while() loop that will loops as variable response equals 1 .

                                               

                        •           Within the inner loop you can use this code.

          

                //The while loop condition allows the user to

                //continue and input another guess if necessary

                while(response==1){

                // tell the user if they are too high or too low

                if(guess<x)

                     System.out.println(\"too low \");

                elseif(guess>x)

                     System.out.println(\"too high \");

                else

                {

System.out.println(\"well done\ \");

break;

}

                System.out.println(\"guess again\ \");

                //input another guess

               

                }

System.out.println(\"You guessed correctly!\");System.out.println(\"Try again?\");

           System.out.println(\"type (1 = yes, 2 = no)? \");

                //input user response

}while(response==1);

   //end inner while loop

   }   

   //end outer while loop

•           You can also limit the user to three guesses.

I have been trying to figure it out but no luck. Please and thank you.

// import a class for random numbers

import java.util.Random;

public class GuessGame

{

     // method to generate a random number

     public static int RandomNumber()

     {

           // local variable to hold a random number

           int rNum = 0;

           // a random object is created

           Random rand = newRandom();

           // a random number is generated

           rNum = rand.nextInt(20) + 1;

           // random number is returned to main()

           return rNum;

     }

     // the main() method

     public static void main(String args[])

     {

           // local variable to hold a random integer     

           int x = 0;

           // local variable is assigned a random number

           x = RandomNumber();

           //display the random number

           System.out.println(x);

     }

}

Solution

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

public class GuessGame {
  
   //Method to return a random number between 1 to 20
   public static int randomNumber(){
       int randomNumber;
       Random rand = new Random();
       randomNumber = rand.nextInt(20) + 1;
       return randomNumber;
   }
  
   public static void guessingGame() throws IOException{
       int currentGuess; // Stores the current Guess of the user
       int response = 1; // Stores the response of the user, whether the user wants to play another round
       System.out.println(\"Welcome to: The Guessing Game\ Have a number \"
               + \"from 1 to 20. Can you guess it?\");
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       // The outer loop is to control the number of games
       do{
           System.out.println(\"Please enter your guess\");
           int randomInteger = randomNumber();
           // The inner loop controls the guesses in one game
           while(true){
               currentGuess = Integer.parseInt(br.readLine());
               if(currentGuess > randomInteger){
                   System.out.println(\"Too High, Guess Again\");
               }
               else if(currentGuess < randomInteger){
                   System.out.println(\"Too Low, Guess Again\");
               }
               else{
                   System.out.println(\"Your guess is correct!!\ Wan\'t to play another round?\ Yes - 1, No - 2\");
                   response = Integer.parseInt(br.readLine());
                   break;
               }
           }
       }while(response == 1); // User plays the game until enters the response as 1
       System.out.println(\"GoodBye!!\");
   }
  
   //The main() method
   public static void main(String args[]) throws IOException{
       guessingGame(); // Starts the Guessing game
   }
  
  
}

Hello, I am having trouble with modifying the following program in Java: // import a class for random numbers import java.util.Random; public class GuessGame {
Hello, I am having trouble with modifying the following program in Java: // import a class for random numbers import java.util.Random; public class GuessGame {
Hello, I am having trouble with modifying the following program in Java: // import a class for random numbers import java.util.Random; public class GuessGame {
Hello, I am having trouble with modifying the following program in Java: // import a class for random numbers import java.util.Random; public class GuessGame {

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site