Hey first time poster, first year of computer programming so I thought to myself, why not sign up for a forum ! So here I am ahah.
Anyways I'm a bit stumped on my assignment, and would like a bit of clarification, I'll show you what I've got so far.
Basically it's the old Jelly Bean game, ask users how many will be playing, and then they enter their names, their guesses and so on.
Here's my code so far.
And here's the method i've created to findLowestDifference() of the users guess and the actual randomized amount of jellybeans.
public class M_H_Project2JellyBeans
public static void main(String args)
Scanner input = new Scanner(System.in);
//introduce the program to the user
System.out.print ( "\t Welcome to Moe's Jelly Bean Guessing Game! " +
"\n\t ******************************************" +
"\n Can you guess correctly how many Jelly Beans are in the jar ? " +
" \n -The number of Jelly Beans in the jar ranges from 1001 to 1999- " );
//ask how many people will be making a guess
System.out.print( "\n\n How many people will be attempting to guess the right number today ? " );
int arraySize = input.nextInt();
//create a String array for the names and an int array for the guess
String nameArray = new String[arraySize];
int guessArray = new int[arraySize];
//create random jelly bean number
int jellyBeans =(int)(Math.random()*(1999-1001+1) + 1001);
//set up a for loop to enter the data
for(int i = 0; i < nameArray.length; i++)
input.nextLine(); //clear the buffer
//ask for name of person
System.out.print("Enter name of person #" + (i+1)+ ":");
nameArray[i] = input.nextLine();
//Ask the user to enter their guess.
System.out.print(nameArray[i] + ", Enter your guess between 1000 and 2000: ");
guessArray[i] = input.nextInt();
//Let user know they've entered an invalid number if number they've entered
//does not range from 1001 to 1999 !
if ( guessArray[i] <= 1000 || guessArray[i] >= 2000 )
System.out.println("Don't be silly ! Your guess has to be between 1001 to 1999...Please try again.");
} while(guessArray[i] <= 1000 || guessArray[i] >=2000);
System.out.println("\t\t\t\t[*" + nameArray[i] + " has entered '" + guessArray[i] + "' as a guess*]" );
if ( guessArray[i] == jellyBeans)
System.out.println("TheThe winner is" + nameArray + "with a guess of " + jellyBeans
+ ", which is exactly the same as the number of jelly beans in the jar.");
System.out.println("\nThere were " + jellyBeans + " Jelly beans in the jar");
int lowestDifference = findLowestDifferencehelper.findLowestDifference(jellyBeans, guessArray);
System.out.println( "Your guess was " + lowestDifference + " jelly beans away from the actual number.");
Now my problem is, when i run the program, and enter let's say 2 users, and enter their guesses, i only get a difference for the first user.
public class findLowestDifferencehelper
*Purpose: determines the difference between actual Jellybeans and players' guess
*Accepts: an array of type int
*Returns: the difference
public static int findLowestDifference(int num1, int num2)
int lowestDifference = Math.abs(num1 - num2);
Welcome to Moe's Jelly Bean Guessing Game!
Can you guess correctly how many Jelly Beans are in the jar ?
-The number of Jelly Beans in the jar ranges from 1001 to 1999-
How many people will be attempting to guess the right number today ? 3
Enter name of person #1:Moe
Moe, Enter your guess between 1000 and 2000: 1300
[*Moe has entered '1300' as a guess*]
Enter name of person #2:Jon
Jon, Enter your guess between 1000 and 2000: 1500
[*Jon has entered '1500' as a guess*]
Enter name of person #3:Steve
Steve, Enter your guess between 1000 and 2000: 1800
[*Steve has entered '1800' as a guess*]
There were 1008 Jelly beans in the jar
Your guess was 292 jelly beans away from the actual number.
I need to create a reportWinner() method, however, how do i make my code give me the lowestdifference for each and every guess ?
This is what the program is supposed to look like when run when i finish.
Thanks in advance guys, I hope I didn't post too much at once ahah.
How many players will be making guesses today? 3
Please enter name # 1: Mike
Mike, enter your guess between 1000 and 2000: 1585
Please enter name # 2: Bill
Bill, enter your guess between 1000 and 2000: 1376
Please enter name # 3: Tony
Tony, enter your guess between 1000 and 2000: 1723
There were 1139 jelly beans in the jar.
The winner is Bill with a guess of 1376, which is 237 away from the actual number of jelly beans.