Question

In: Computer Science

In Java: Write a program that generates a random number and asks the user to guess...

In Java: Write a program that generates a random number and asks the user to guess the number and keeps track of how many guesses it took

  • If the user input is negative or zero then the loop must stop reading further inputs and display how many guesses they used
  • If they guess the correct number display a message telling them they got it and exit the program
  • If they guess the wrong number (but still a legal guess) you should tell them if they are too high or too low.
  • You may choose to assign a limit to the range of the random number (ex. limit to numbers between 1 and 100, 1 and 1000, etc.)
  • Use JOptionPane for all input and output
  • Validate input to only accept integer values. If a non-integer is entered you should display an error message and prompt the user to try again – the count of how many guesses they’ve made should not be interrupted and the program should gracefully continue once valid entry is received.
  • Hint: import java.util.Random and use Random rand = new Random(); to declare the rand variable and then use numberToGuess = rand.nextInt(x); to generate a random number (replace x with the upper limit you want to impose on your random number).
  • You may write this with all code in the main method or you may use user generated methods.

Solutions

Expert Solution

Explanation:I have written the code in RandomGuess class and have used the JOPtionPane in the main method to accept and display the user input and output.I have also shown the output of the program, please find the images attached with the answer.Please upvote if you liked my answer and comment if you need any modification or explanation.

//code starts

import java.util.Random;

import javax.swing.JOptionPane;

public class RandomGuess
{
   public static void main(String[] args)
   {

       Random random = new Random();
       int numberToGuess = random.nextInt(100) + 1;
       int guessCount = 0;
       while (true)
       {
           try
           {
               guessCount++;
               int x = Integer.parseInt(JOptionPane.showInputDialog("Guess a number between 1 and 100? "));
               if (numberToGuess == x)
               {
                   JOptionPane.showMessageDialog(null, "Your guessed it correct by guessing " + guessCount + " times");
                   break;
               }
               if (x == 0 || x < 0)
               {
                   JOptionPane.showMessageDialog(null, "you made " + guessCount + " guesses");
                   break;
               }
               if (x < numberToGuess)
               {
                   JOptionPane.showMessageDialog(null, "Your guess is too low, try again");
                   continue;
               } else if (x > numberToGuess)
               {
                   JOptionPane.showMessageDialog(null, "Your guess is too high, try again");
                   continue;
               }
           } catch (NumberFormatException e)
           {
               JOptionPane.showMessageDialog(null, "Please enter a valid integer");
               continue;
           }

       }

   }
}

Output:


Related Solutions

17. Write a program that generates a random number and asks the user to guess what...
17. Write a program that generates a random number and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. 18. Enhance the program that you wrote for Programming...
Write a program that generates a random number between 1 and 100 and asks the user...
Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Your program should also keep a...
Write a program that generates a random number between 1 and 50 and asks the user...
Write a program that generates a random number between 1 and 50 and asks the user to guess it. As a hint, it tells the user how many divisors it has, (excluding 1 and the number itself). Each time the user makes a wrong guess, it displays "Wrong" and says if the guess was too low or too high, until the user gets it right, in which case it says "Right" and says how many trials it took to guess...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
Write a C program that asks the user to guess a number between 1 and 15(1...
Write a C program that asks the user to guess a number between 1 and 15(1 and 15 are included). The user is given three trials. This is what I have so far. /* Nick Chioma COP 3223 - HW_2 */ #include <iostream> #include <time.h> // needed for time function #include <stdlib.h> // needed for srand, rand functions int main () {       int numbertoguess, num, correct, attempts;       srand(time(NULL)); //this initializes the random seed, making a new...
Write a java program that asks the user for a number n and gives them the...
Write a java program that asks the user for a number n and gives them the possibility to choose between computing the sum and computing the product of 1,…,n. Example of running this program: Enter an integer number n: __7________ Enter Sum or Product: __Sum__________________________________ Program output: Sum of 1 ... 7 Sum or Product: Sum Sum = 28 Now second sample of second execution Enter an integer number n: __5__________________________________ Enter Sum or Product: __Product__________________________________ Program output:  Product of 1...
C++. Write a program that generates a random number between 5 and 20 and asks the...
C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses the user makes until...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to exit)”. Your program shall accept integers from the user (positive or negative), however, if the user enters 0 then your program shall terminate immediately. After the loop is terminated, return the total sum of all the previous numbers the user entered. a. What is considered to be the body of the loop? b. What is considered the control variable? c. What is considered to...
Write a Java program that calculates a random number 1 through 100. The program then asks...
Write a Java program that calculates a random number 1 through 100. The program then asks the user to guess the number.If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.The program must let the user continue to guess until the user correctly guesses the number. ★Modify the program to output how many guesses it took the user to correctly guess the right number
This is Java In this problem we will write a program that asks the user to...
This is Java In this problem we will write a program that asks the user to enter a) The user's first name and b) a series of integers separated by commas. The integers may not include decimal points nor commas. The full string of numbers and commas will not include spaces either, just digits and commas. An example of valid input string is:        7,9,10,2,18,6 The string must be input by the user all at once; do not use a loop...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT