【问题标题】:How to count for an amount of specific items in an ArrayList in Java如何在 Java 中计算 ArrayList 中特定项目的数量
【发布时间】:2015-11-20 06:35:32
【问题描述】:

因此,我最近决定使用 Java 编程重新创建“骰子 10,000”游戏,但在让某些规则起作用时遇到了一些问题。有 4 条规则我无法开始工作。

1) 任意数字中的 3 = 3 个骰子面值的 10 倍

2) 完全顺子 (1 - 6) = 3,000 分

3) 3 对 = 1,000 分

4) 3 个一 = 1,000 分

我只是不确定如何让这些工作......我相信我会使用集合,但我对集合以及如何使用它们以及它们包含哪些方法非常非常缺乏经验。没有一套我大概能猜出第二个,但最麻烦的是第一个、第三个和第四个。

//Andrew M
import java.util.*;
import java.io.*;
class Zilch
{

    public static Random r = new Random();
    public static ArrayList<Integer> diceList = new ArrayList<Integer>();
    public static int count = 0, humanScore = 0, compScore = 0, humanBank = 0, compBank = 0, lastMove = 0;
    public static ArrayList<Integer> diceKeep = new ArrayList<Integer>();
    public static boolean gameOn = true, x = true;
    //-----------------------------------------------------------------------------------
    public static void main(String[] args)
    {
        for (int j = 1; j <= 6; j++)
        {
            diceList.add(j);
            diceKeep.add(null);
        }
        do
        {
            do
            {
                x = true;
                for (int k = 0; k < diceKeep.size(); k++)
                {
                    if (diceKeep.get(k) != null)
                    {
                        System.out.println("Dice #" + (k + 1) + ": " + diceKeep.get(k) + "  -- kept.");
                    }
                }
                System.out.println();
                System.out.println();
                boolean b = true;
                for (int i = 0; i < diceList.size(); i++)
                {
                    if(diceList.get(i) != 0)
                    {
                        diceList.set(i, r.nextInt(6) + 1);
                        System.out.println("Dice #" + (i+1) + " is a " + diceList.get(i));
                    }
                }
                System.out.println();
                System.out.println("Select which dice to roll again... (Select one at a time, 1 - 6), then type \"go\" to complete your turn.");
                Scanner input = new Scanner(System.in);

                /*-----------UNNECCESARY CODE--------------*/
                    else
                    if (selection.equals("go"))
                    {
                        System.out.println();
                        System.out.println();
                        if (diceKeep.get(0) == 1)
                        {
                            humanBank = humanBank + 100;
                            lastMove = 100;
                        }
                        else
                        if (diceKeep.get(0) == 5)
                        {
                            humanBank = humanBank + 500;
                            lastMove = 500;
                        }

/*--------------I NEED THE RULES TO GO HERE---*/        

                        b = false;
                    }
                }
                while(b == true);
                //-----------------------------------------------------------------------
                if(gameOn == true)
                {
                    x = true;
                    //Place Score Card Here
                }
            }
            while (x == true);
        }
        while (1< 2); //Just here for testing purposes, for now
    }
}

对不起,我知道现在很多。希望你们能给我一些示例代码让我继续前进吗?非常感谢。

一切顺利

安德鲁

编辑:截至目前,我要到明天早上才能回复。我为缺乏反馈而道歉,但为时已晚。我明天会回来尝试和回复。

【问题讨论】:

  • 有很多方法可以做到这一点。如果是我,我会使用 Map&lt;Integer, Integer&gt; 配对,其中键是骰子的数字,值是出现的次数。
  • 嗯,我想把它放在一个 if 语句中,比如if(/*There are three fives*/) 另外,你能发布一个描述如何使用地图的答案吗?如果我的问题被它解决了,我很乐意接受它。不过,现在我要睡觉了。我明天会反馈。
  • 使用Map&lt;Integer, Integer&gt;,您可以遍历这些值并查看它们中的任何一个是否大于 6(三对)或检查地图是否只有一个条目(完整的顺子)。
  • 我,我刚开始,所以我不知道 Map 是如何工作的。我会考虑如何手工完成,而不考虑编程。例如,您如何检查规则 1?我在想,看看有多少个骰子与第一个骰子相同(看看这个数是否为 3(或更多?));然后看 2 到 6 中有多少与第二个骰子相同,然后看 3 到 6 中有多少与第三个骰子相同,然后 4 到 6 中有多少与第四个相同。我相信,两个循环(一个嵌套在另一个循环中)可以为您做到这一点。
  • 顺便说一下,请注意diceKeep 中的所有值在分配之前都是null。因此,如果玩家保留 5 号骰子,那么 1 - 4 号骰子将是null,直到“保留”。因此,我不能使用 Collections.sort(diceKeep) 或 BubbleSort 或选择排序(全部抛出 NullPointerException

标签: java arraylist set counting


【解决方案1】:

在下面,我添加一些评论。它打印数组列表中的数字计数。

import java.util.ArrayList;

public class Test {

    public static void main(String[] args) {
        int [] i = new int [7]; //  array starts with 0, so we can init our array with size 7 (0 to 6).
        ArrayList<Integer> arr = new ArrayList<Integer>();

        // there are 2 times "5"
        arr.add(5); 
        arr.add(5);

        //add some null
        arr.add(null);

        // there is 1 times "3"
        arr.add(3);

        // there is 1 times "6"
        arr.add(6);

        //add some null
        arr.add(null);

        System.out.println("numbers in arr: " + arr);

        for (Integer integer : arr) {
            if (integer!= null)
                i[integer]++; 
        }

        for (int c = 0 ;c<i.length ; c++){
            System.out.println("there are/is " + c + " ->" + i[c] + " times");
        }
    }
}

打印:

numbers in arr: [5, 5, null, 3, 6, null]
there are/is 0 ->0 times
there are/is 1 ->0 times 
there are/is 2 ->0 times 
there are/is 3 ->1 times 
there are/is 4 ->0 times 
there are/is 5 ->2 times 
there are/is 6 ->1 times

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-20
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    • 2019-03-15
    • 2017-07-21
    • 2017-06-27
    相关资源
    最近更新 更多