Write a program that plays a guessing game with the user Spe

Write a program that plays a guessing game with the user. Specifically, your program should randomly pick a number between 1 and 100. Then, ask the user for a guess. You should detect and tell the user if the guess is not a valid guess. Otherwise, tell the user their guess was too high or too low. The program should continue to prompt the user for new guesses until they get the correct number, telling them each time if the guess was too high or too low or invalid.

Here are a couple development/debugging strategies for this “target” variable:

•Print out the random number, to make sure your program is acting correctly– remember to remove/comment this before running unit tests/submitting.

•Temporarily set the random “seed” to a value, which will have the effect of always choosing the same random number–the unit tests have fixed seeds that you can use with known outcomes.

•Temporarily set the “target” variable to a fixed number, so you can test to see how your program responds in different testing situations.

Base code:

package edu.wit.cs.comp1000;

import java.util.Random;

//TODO: document this class
public class PA4b {

   // TODO: document this method
   public static void main(String[] args) {
      
       //////////////////////////////////////////////////////////////////////////////
       // DO NOT CHANGE IN FINAL SUBMISSION
       //////////////////////////////////////////////////////////////////////////////
      
       Long seed;
       if (args.length != 1) {
           seed = null; // you can temporarily change this to assist in debugging
                         // the value must end in L, such as 1070L
       } else {
           seed = Long.valueOf(args[0]);
       }
      
       // Gets a random number between 1 and 100
       // Use the target variable as the correct answer for guessing
       Random random;
       if (seed == null) {
           random = new Random();
       } else {
           random = new Random(seed);
       }
       int target = (Math.abs(random.nextInt()) % 100) + 1;
      
       //////////////////////////////////////////////////////////////////////////////
       //////////////////////////////////////////////////////////////////////////////
      
       // It might be useful to see the correct answer
       // TODO: delete this line before attempting unit tests or submitting work
       System.out.println(target);
      
       // TODO: write your code here      
   }

}

Solution

// Guessing game PA4b.java

import java.util.Scanner;
import java.util.Random;
public class PA4b {

      public static void main(String[] args) {

      Scanner keyboard = new Scanner(System.in);

      Long seed;
      if (args.length != 1)
      {
            seed = null;
      }
      else
      {
            seed = Long.valueOf(args[0]);
      }

      int target;
      int guessNumber;

      Random random;
      if (seed == null)
      {
            random = new Random();
      }
      else
      {
            random = new Random(seed);
      }
      target = (Math.abs(random.nextInt()) % 100) + 1;

      System.out.println(\"Guessing Game: Guess the number between 1 and 100!\ \");
    
      while (true)
      {    
            System.out.print(\"\ Enter a guess: \");
            guessNumber = keyboard.nextInt();

            if(guessNumber < 1 || guessNumber > 100)
            {
                  System.out.println(\"Invalid Input\ \");
                  continue;
            }

            else if (guessNumber == target)
            {
                  System.out.println(\"Well Done! You guessed it!\ \");
                  break;
            }

            else if (guessNumber < target)
                  System.out.println(\"Too Low!\ \");

            else if (guessNumber > target)
                  System.out.println(\"Too High!\ \");

        }

      }

}

/*
output:

Guessing Game: Guess the number between 1 and 100!


Enter a guess: 34
Too Low!


Enter a guess: 50
Too High!


Enter a guess: 40
Too High!


Enter a guess: 38
Too High!


Enter a guess: 36
Too High!


Enter a guess: 35
Well Done! You guessed it!

*/

Write a program that plays a guessing game with the user. Specifically, your program should randomly pick a number between 1 and 100. Then, ask the user for a g
Write a program that plays a guessing game with the user. Specifically, your program should randomly pick a number between 1 and 100. Then, ask the user for a g
Write a program that plays a guessing game with the user. Specifically, your program should randomly pick a number between 1 and 100. Then, ask the user for a g

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site