Java猜测游戏项目(初学者)

时间:2015-03-27 19:38:08

标签: java arrays if-statement methods

我目前正在开发一个java项目,并且我应该创建一个生成1000到2000之间的数字的程序,然后让你有机会输入玩家的数量,接受所有的名字和猜测在两个不同的数组中,然后创建两个方法,一个用于查找与实际数字最接近的猜测,另一个用于报告或找到胜利者与其猜测匹配并将其打印出来。我的第二种方法有问题,我现在在逻辑部分画一个空白,这是我的代码和输出:

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.");
            }
        }
    }
    }

当有多个获胜者时,这是它产生的输出。

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.

这应该是什么样子

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.

我遇到方法部分有问题,我知道这么多,但如果你需要看到其余的代码让我知道任何帮助将不胜感激,谢谢。 (我想我需要一种方法来取消第一次打印,如果计数器通过一个,但我不知道如何去做)

2 个答案:

答案 0 :(得分:2)

试试这个。我在代码中做了很多更改,因为我不理解它。以下更改:

  • 没有原始类型
  • 只有一个循环才能找到胜利者
  • 只有一个带有播放器的数组及其数字
  • 输出的不同循环

这是代码

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;
        }
    }

}

答案 1 :(得分:0)

这就是你需要的。注意:建议使用有意义的变量名而不是arraytempList来使代码更具可读性。

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);
    }
}