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 = 0; //Number of jellybeans in jar
int guess = 0; //The user's guess
Random generator = new Random();
Scanner scan = new Scanner (System.in);
//randomly generate the number of jellybeans in jar
numOfJellyBeans = generator.nextInt(999)+1;
System.out.println("There are between 1 and 1000 jellybeans in the jar,");
do
{
System.out.print("Enter your guess: ");//prompt user to quess and read in
guess = scan.nextInt();
if(guess < numOfJellyBeans) //if the quess is wrong display message
{
System.out.println("Too low.");
}
else if(guess > numOfJellyBeans);
{
System.out.println("Too High.");
}
else
{
System.out.println("You got it"); // display message saying guess is correct
}
} while (guess != numOfJellyBeans);
}
}