3 骰子总和计算程序 Java

3 Dice Sum Counting Program Java

对于我的计算机科学 Class,我的老师要求我们做以下事情:

程序说明:您正在学习玩一个涉及 3 个六面骰子的新游戏。你知道,如果你知道每次可能掷骰子的概率,你就会成为一个更好的竞争对手。

由于您刚刚学习了数组并使用它们来计算多个项目,因此编写此程序应该是轻而易举的事。这会很酷,因为我们上次这样做时我们只是在寻找 9 或 10 可以滚动多少次,这个程序不需要任何 if 语句。

所需语句:输出、循环控制、数组

示例输出:

可能的组合数

1 0

2 0

3 1

4 3

5 6

6 10

7 15

8 21

9 25

10 27

11 27

12 25

13 21

14 15

15 10

16 6

17 3

18 1

我可以使用 if 语句轻松地做到这一点,但我不明白没有它该怎么做。这特别棘手,因为在提示下,她写道:"These programs utilize a counting array. Each time a value is generated the position at that index is incremented. It’s like the reverse of the lookup table."我不知道这是什么意思。

这是我的带有 if 语句的代码:

public class prog410a
{
    public static void main(String args[])
    {
        System.out.println("Number\tPossible Combinations");

        for (int x = 1; x <= 18; x++)
        {
            int count = 0;
            for (int k = 1; k <= 6; k++)
            {
                for (int i = 1; i <= 6; i ++)
                {
                    for (int j = 1; j <= 6; j++)
                    {
                        if (k + i + j == x)
                            count++;
                    }
                }
            }
            System.out.println(x + "\t\t\t" + count);
        }

    }
}

所以我想我的总体问题是:我怎样才能模拟这个,但是通过使用某种数组而不是 if 语句?

您不需要外部 x 循环。您所需要的只是三个嵌套循环,一个用于每个骰子。您还需要一个全部初始化为零的整数数组。在最里面的骰子循环中,您只需使用三个骰子的总和作为整数数组的索引并增加该索引处的值。

完成骰子循环后,您可以迭代整数数组并输出结果的频率。

既然是作业,就不给你写代码了,给你大概的大纲吧。

创建一个大小为 18 的计数数组。将所有值初始化为 0。

拥有三个从 1 到 6 的嵌套循环,与您的三个内部循环完全相同。这些代表您骰子上的值。

在最里面的循环中,将三个循环计数器加在一起。这是您的骰子总数,您将其用作计数数组的索引以增加该索引处的值。

退出三个嵌套循环后,使用另一个循环遍历计数数组以打印出值。

这似乎可行 - 并且没有 if:

public void test() {
    // Remember to -1 because arrays are accessed from 0 to length-1
    int[] counts = new int[18];
    // Dice 1.
    for (int k = 1; k <= 6; k++) {
        // Dice 2.
        for (int i = 1; i <= 6; i++) {
            // Dice 3.
            for (int j = 1; j <= 6; j++) {
                // Count their sum (-1 as noted above).
                counts[i + j + k - 1] += 1;
            }
        }
    }
    // Print out the array.
    System.out.println("Number\tPossible Combinations");
    for (int i = 0; i < counts.length; i++) {
        System.out.println("" + (i + 1) + "\t" + counts[i]);
    }
}

本质上,您将结果构建在一个数组中,然后将它们输出。

来自维基百科:In computer science, a lookup table is an array that replaces runtime computation with a simpler array indexing operation. The savings in terms of processing time can be significant, since retrieving a value from memory is often faster than undergoing an 'expensive' computation or input/output operation.,这意味着通常我们使用查找 tables 来节省计算时间,方法是将一些过程预先计算到我们已经存储结果的 table 中。在这种情况下,您使用该过程将可能结果的数量存储在一个数组中。基本上,您正在为骰子结果构建查找 table。只需要三个内部循环。

        for (int k = 1; k <= 6; k++)
        {
            for (int i = 1; i <= 6; i ++)
            {
                for (int j = 1; j <= 6; j++)
                {
                    arr[k + i + j-1] = arr[k + i + j-1] +1;
                }
            }
        }

这就是正在发生的事情:

dices   index
i j k  (i+j+k)
1 1 1     3
1 1 2     4
1 1 3     5
1 1 4     6
1 1 5     7
1 1 6     8
1 2 1     4
1 2 2     5
1 2 3     6
1 2 4     7
1 2 5     8
1 2 6     9
1 3 1     5
1 3 2     6
.
.
.

您正在枚举每个可能的结果,然后将生成的索引添加到数组中。嵌套循环完成后,您将得到一个包含所需信息的数组。