Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I am supposed to write a program in Java using a do while loop that prompts the user to guess the correct number of jelly
I am supposed to write a program in Java using a do while loop that prompts the user to guess the correct number of jelly beans in a jar. When they get it right, it's supposed to ask if they want to play again. If yes, the loop starts over. I don't know how to prompt the user for a y or n or where to put it within the loop and continue the game without running in to a lot of problems. Please help. I've included my code and description of the problem.
You'll be writing a program called JellyBeanGame.java that lets you play this game. Your program should randomly generate a number from 1 - 1000 to represent the number of jellybeans in the jar. Then your program should ask the user to try and guess the number of jellybeans. If the user guesses incorrectly, the program should ask them to try again until the guess is correct; when the guess is correct, the program should print a congratulatory message! Use a do ... while loop. Finally, keep prompting the user to ask if they want to play again. 7 import java.util. Random; 8 import java.util.Scanner; 10 public class JellyBeanGame 11 { 12e public static void main(String[] args) 13 14 //Define integer int numOfJellyBeans = 0; int guess = 0; int guesses = 0; //randomly generate the number of jellybeans in jar from 1-1000 Random gen = new Random(); numOfJellyBeans = gen.nextInt(1000) + 1; System.out.println("There are between 1 and 1000 jellybeans in the jar."); System.out.println("How many do you think are in the jar?"); do 65 //prompt user and read in guess Scanner scan = new Scanner (System.in); System.out.print("Enter your guess: "); guess = scan.nextInt(); guesses++; //Lets you print out the number of guesses when answered correctly //if the guess is wrong display message if (guess numOfJellyBeans) { System.out.println("Too high!"); System.out.println("");} else {System.out.println("High Five! You got it, after " + guesses + " guesses!!");} while (guess != numOfJellyBeans)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started