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

80.2K

Verified Solution

Question

Programming

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

  • If the user input is negative or zero then the loop must stopreading further inputs and display how many guesses they used
  • If they guess the correct number display a message telling themthey got it and exit the program
  • If they guess the wrong number (but still a legal guess) youshould tell them if they are too high or too low.
  • You may choose to assign a limit to the range of the randomnumber (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-integeris entered you should display an error message and prompt the userto try again – the count of how many guesses they’ve made shouldnot be interrupted and the program should gracefully continue oncevalid entry is received.
  • Hint: import java.util.Random and use Random rand =new Random(); to declare therand variable and then use numberToGuess =rand.nextInt(x); to generate a random number (replace x withthe upper limit you want to impose on your random number).
  • You may write this with all code in the main method or you mayuse user generated methods.

Answer & Explanation Solved by verified expert
4.2 Ratings (686 Votes)
ExplanationI have written the code in RandomGuess classand have used the JOPtionPane in the main method to accept anddisplay the user input and outputI have also shown the output ofthe program please find the images    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students