Ok so... I have a problem with completing my code for class tomorrow. I need help with this ASAP. I'm asked to implement a program that generates a random number of jelly beans in a jar, prompt the user to make a guess on how many jelly beans are in the jar, and count how many times the user tried to guess before getting it right.

That's my problem right there -- getting the program to count how many times the user inputted a guess. Here's my code:

import java.util.Scanner;
import java.util.Random;

public class JellyBeanGame
    public static void main(String[] args)
        int numOfJellyBeans;       //Number of jellybeans in jar
        int guess = 0;                       //The user's guess

		 Random generator = new Random();
		 numOfJellyBeans = generator.nextInt(999)+1;
		 Scanner scan = new Scanner (System.in);

		//randomly generate the number of jellybeans in jar

		System.out.println("There are between 1 and 1000 jellybeans in the jar,");

	    System.out.print("Enter your guess: ");//prompt user to guess and read in 
		 guess = scan.nextInt();
		 if(guess < numOfJellyBeans)//if the guess is wrong display message
					System.out.println("Too low.");
		 else if(guess > numOfJellyBeans)
		 			System.out.println("Too High.");
		 			System.out.println("You got it"); // display message saying guess is correct
	}	while (guess != numOfJellyBeans);


I'm not sure what I have to do with the while loop either, that's why there's nothing there.

If anyone could please tell me how to do this, I'd greatly appreciate it. It's due tomorrow morning so I have to MOVE ON IT. I've Googled many different ways of wording my question such as: How to count number of user inputs in java?


Create a new variable guess count which keeps track of the number of times the user guesses the bean count.

do {
  if(reply == jellyCount) {
    // break and congratulate user
  } else {
    // increment "guessCount" and show the failure msg to user
} while(true);
// since we have while(true) make sure you provide the user an option
// to quit in between the "game"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.