Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-30-2010, 02:49 AM   PM User | #1
hmafia
New to the CF scene

 
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
hmafia is an unknown quantity at this point
Method Help- Checking Against more than 1 user!

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.

Code:
import java.util.Scanner;

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];
        int xNumber;
                  //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();
            
                   //do-while loop
            do
       {
           //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[0]);
            
       
       System.out.println( "Your guess was " + lowestDifference + " jelly beans away from the actual number.");
      
        
                 
            
                 
        
      
      }

}
And here's the method i've created to findLowestDifference() of the users guess and the actual randomized amount of jellybeans.

Code:
public class findLowestDifferencehelper
{

    /*
     *MethodName: findLowestDifference()
     *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);
        
        return lowestDifference;
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.

Code:
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.

Code:
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.
Thanks in advance guys, I hope I didn't post too much at once ahah.
hmafia is offline   Reply With Quote
Old 11-30-2010, 04:11 AM   PM User | #2
hmafia
New to the CF scene

 
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
hmafia is an unknown quantity at this point
Alright I figured out how to make it read the difference between the Jelly beans and the guess for each user, how do I get started on determining who the winner is based on the closest guess ?

I've posted what the code should look like when it's completed and executed above so any help would be much appreciated, only got a day left before I hand this in.
hmafia is offline   Reply With Quote
Old 11-30-2010, 07:17 AM   PM User | #3
pigpen
Regular Coder

 
Join Date: Dec 2007
Posts: 137
Thanks: 1
Thanked 21 Times in 21 Posts
pigpen is on a distinguished road
You'll have to loop through your guess array and compare the values, using your lowestDifference method to see which is the lowest.

On each loop iteration, you'll have to compare the current lowestDifferenceValue with the previous value, like to a variable called previousDifferenceValue. If lowestDifferenceValue is less than the previous value, then store value of lowestDifferenceValue to previousDifferenceValue.

Outside of the loop, before it starts, you'll want to set the previousDifferenceValue to the total number of jelly beans, since the first guess will always be less than the total number of jelly beans.

For the type of loop I suggest doing a "for" loop, like (int i = 0; i < guessArray.length; i++) {

This way you'll have an "i" variable to store the indexes in the array, like guessArray[i] is a match, then you can save it to a winnerNameIndex so you'll know the winner's name, too. Make sure you declare winnerNameIndex outside of the loop.

That way at the end, after the loop is done, you can declare the winner by doing calling namesArray[winnerIndexName];

Here's some example code. This goes at the inside your main method, at the end. You'll have to format it to fit your specifications, or break it the code out into it's own method. It reports all the guess and just declares the winner by name.

Code:
       int previousLowestDifference = jellyBeans;
       int winnerNameIndex = 0;
       
       for (int i=0; i < guessArray.length; i++) {
         int lowestDifference = findLowestDifferencehelper.findLowestDifference(jellyBeans, guessArray[i]);
         System.out.println( nameArray[i] + "'s guess was " + lowestDifference + " jelly beans away from the actual number.");
         
         // check for winner by comparing previous lowest difference value with current lowest difference value
         if (lowestDifference < previousLowestDifference) {
           previousLowestDifference = lowestDifference;
           winnerNameIndex = i;
         }
       
       }
       System.out.println("Winner is " +nameArray[winnerNameIndex]);
       //System.out.println( "Your guess was " + lowestDifference + " jelly beans away from the actual number.");
pigpen is offline   Reply With Quote
Old 12-01-2010, 12:16 AM   PM User | #4
hmafia
New to the CF scene

 
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
hmafia is an unknown quantity at this point
Quote:
Originally Posted by pigpen View Post
You'll have to loop through your guess array and compare the values, using your lowestDifference method to see which is the lowest.

On each loop iteration, you'll have to compare the current lowestDifferenceValue with the previous value, like to a variable called previousDifferenceValue. If lowestDifferenceValue is less than the previous value, then store value of lowestDifferenceValue to previousDifferenceValue.

Outside of the loop, before it starts, you'll want to set the previousDifferenceValue to the total number of jelly beans, since the first guess will always be less than the total number of jelly beans.

For the type of loop I suggest doing a "for" loop, like (int i = 0; i < guessArray.length; i++) {

This way you'll have an "i" variable to store the indexes in the array, like guessArray[i] is a match, then you can save it to a winnerNameIndex so you'll know the winner's name, too. Make sure you declare winnerNameIndex outside of the loop.

That way at the end, after the loop is done, you can declare the winner by doing calling namesArray[winnerIndexName];

Here's some example code. This goes at the inside your main method, at the end. You'll have to format it to fit your specifications, or break it the code out into it's own method. It reports all the guess and just declares the winner by name.

Code:
       int previousLowestDifference = jellyBeans;
       int winnerNameIndex = 0;
       
       for (int i=0; i < guessArray.length; i++) {
         int lowestDifference = findLowestDifferencehelper.findLowestDifference(jellyBeans, guessArray[i]);
         System.out.println( nameArray[i] + "'s guess was " + lowestDifference + " jelly beans away from the actual number.");
         
         // check for winner by comparing previous lowest difference value with current lowest difference value
         if (lowestDifference < previousLowestDifference) {
           previousLowestDifference = lowestDifference;
           winnerNameIndex = i;
         }
       
       }
       System.out.println("Winner is " +nameArray[winnerNameIndex]);
       //System.out.println( "Your guess was " + lowestDifference + " jelly beans away from the actual number.");


Thanks a lot, that helped out and will help out in the future. Will definitely study that more.
hmafia is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:06 AM.


Advertisement
Log in to turn off these ads.