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

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 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...
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...
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...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements...
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements and create an array. Then, write a Merge Sort function with recursion (in the main) that takes the user inputted elements in the array, sorts them, and prints them back.
Write a Java program that lets the user play a game where they must guess a...
Write a Java program that lets the user play a game where they must guess a number between zero and one hundred (0-100). The program should generate a random number between 0 and 100 and then the user will try to guess the number. The program should help the user guess the number (see details below). The program must allow the user five attempts to guess the number. Further Instruction: (a) Declare a variable diff and assign to it the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT