user4722093
user4722093

Reputation:

Java Guessing Game Project (Beginner)

I'm currently working on a java project, and im supposed to make a program that generates a number between 1000 and 2000, then gives you the chance to enter the amount of players, takes in all the names and guesses in two different arrays, then create two methods, one to find the closest guess to the actual numbers and another to report or find the winner match it up with their guess and print it out. Im having trouble with my second method, im drawing a blank on the logic part right now this is my code and output:

public static void reportWinner (int answer, int winner, int [] array, String [] array1)
{
    ArrayList tempList = new ArrayList();
    ArrayList tempList1 = new ArrayList();
    int counter = 0;
    int temp = 0;
    for(int index = 0; index < array.length; index++)
    {
        temp = answer - array[index];
        temp = Math.abs(temp);
        if(temp == winner)
        {
            counter++;
            tempList.add(array1[index]);
            tempList1.add(array[index]);
        }
    }
    for(int index = 0; index < tempList.size(); index++)
    {
        if(tempList.size() == 1);
        {
            System.out.println("The winner is " + tempList.get(0) + ", with a guess of " + tempList1.get(0) + " which is " 
                             + winner + " away from the actual number of jelly beans.");
            if(tempList.size() > 1)
            {
                System.out.println("The winners are: ");
                System.out.println(tempList.get(index) + ", with a guess of " + tempList1.get(index) + " which is " 
                                   + winner + " away from the actual number of jelly beans.");
            }
        }
        if(tempList.size() == 1 && winner == 0)
        {
            System.out.println("The winner is " + tempList.get(0) + ", with a guess of " + tempList1.get(0) + " which is " 
                             + "the exact number of jelly beans in the jar.");
            if(tempList.size() > 1)
            {
                System.out.println("The winners are: ");
                System.out.println(tempList.get(index) + ", with a guess of " + tempList1.get(index) + " which is " 
                                    + "the exact number of jelly beans in the jar.");
            }
        }
    }
    }

This is the output it is producing when there is more than one winner.

There were 1532 jelly beans in the jar.
The winner is Stan, with a guess of 1200 which is 332 away from the actual number of jelly beans.
The winners are: 
Stan, with a guess of 1200 which is 332 away from the actual number of jelly beans.
The winner is Stan, with a guess of 1200 which is 332 away from the actual number of jelly beans.
The winners are: 
greg, with a guess of 1200 which is 332 away from the actual number of jelly beans.
The winner is Stan, with a guess of 1200 which is 332 away from the actual number of jelly beans.
The winners are: 
Jim, with a guess of 1200 which is 332 away from the actual number of jelly beans.

This is what it should look like

There were 1500 jelly beans in the jar.

The winners are:

Mike with a guess of 1475, which is 25 away from the actual number of jelly beans.
Tony with a guess of 1525, which is 25 away from the actual number of jelly beans.

Im having trouble with the method portion i know this much but if you need to see the rest of the code let me know any help would be appreciated, thanks. (I think i need a way to void the first print out if the counter passes one but im not sure how to go about this)

Upvotes: 0

Views: 960

Answers (2)

drkunibar
drkunibar

Reputation: 1337

Try this. I have made a lot of changes in your code because I did not understand it. Following changes:

  • No raw types
  • Only one loop to find the winner
  • Only one array with the player and their numbers
  • A different loop for the output

Here is the code

import java.util.ArrayList;
import java.util.List;

public class GuesNumber {

    public static void main(String[] args) {
        // The number we want to guess
        final int theNumber = 1500;
        // A random set of players and their numbers
        List<Player> players = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            players.add(new Player("Player " + i, (int) (Math.round((Math.random() * 1000)) + 1000)));
        }
        // Call of your function (with less arguments)
        reportWinner(theNumber, players);
    }

    public static void reportWinner(int theNumber, List<Player> players) {
        // Here we store the minimal diff between THE number 
        // and the number of the potential winner
        Integer minDiff = null;
        // Here we store the winners
        ArrayList<Player> winners = new ArrayList<>();
        // Find the winners
        for (Player player : players) {
            int diff = Math.abs(player.number - theNumber);
            // We have a potential winner the player is the first 
            // we check or his number is less or equal then 
            // the minimal diff
            if (minDiff == null || minDiff <= diff) {
                // We have to clear all potential winners, 
                // if the number of the player is smaller then
                // the minimal diff
                if (minDiff != null && minDiff > diff) {
                    winners.clear();
                }
                // Add a potential winner
                winners.add(player);                    
                minDiff = diff;
            };
        }
        // Let's do the output
        System.out.println("There were " + theNumber + " jelly beans in the jar\n");
        System.out.println("The winner " + (winners.size() == 1 ? "is" : "are") + ":\n");
        for (Player player : winners) {
            System.out.print(player.name + " with a gues of " + player.number + ", wich is " + minDiff + " away from the actual number of jelly beans. ");
        }
        System.out.println("");
    }

    public static class Player {

        String name;
        int number;

        public Player(String name, int number) {
            this.name = name;
            this.number = number;
        }
    }

}

Upvotes: 2

Kedarnath Calangutkar
Kedarnath Calangutkar

Reputation: 534

This is what you need. Note: it is recommended to use meaningful variable names rather than array or tempList to make the code more readable.

public static void reportWinner (int answer, int winner, int [] array, String [] array1)
{
    ArrayList tempList = new ArrayList();
    ArrayList tempList1 = new ArrayList();
    int temp = 0;
    for(int index = 0; index < array.length; index++)
    {
        temp = answer - array[index];
        temp = Math.abs(temp);
        if(temp == winner)
        {
            tempList.add(array1[index]);
            tempList1.add(array[index]);
        }
    }
    if(tempList.size() > 1) {
        System.out.println("The winners are: ");
    } else if(tempList.size() == 1) {
        System.out.println("The winner is: ");
    }
    for(int index = 0; index < tempList.size(); index++)
    {
        String suffix = "";
        if(winner == 0) {
            suffix = "the exact number of jelly beans in the jar."
        } else {
            suffix = winner + " away from the actual number of jelly beans."
        }
        System.out.println(tempList.get(index) + ", with a guess of " + tempList1.get(index) + " which is " + suffix);
    }
}

Upvotes: 0

Related Questions