【问题标题】:Balut dice game Java巴鲁特骰子游戏 Java
【发布时间】:2019-07-05 23:02:01
【问题描述】:

我正在为我的大学的额外作业制作一个游戏,我必须创建一个名为“balut”的骰子游戏我在为数组分配值以及将骰子值存储在其中时遇到了一些问题大批。

我在我的课程第 11 周的第 9 周,我们已经介绍了数组和方法,但这对我来说是一个新概念。目标如下:

Balut = all five dice have the same number. 
Straight =  a total of 15 Or 20.
Sixes = 1 or more sixes.
Fives = 1 or more fives.
Fours = 1 or more fours.
10 rounds. 
Total scoring of categories. 
total of scores. 
If no category is met "none" is printed.

我已经为此投入了至少 14 个小时,而且它原本是一个 6 到 8 个小时的课程,但我仍然在苦苦挣扎,质疑我是否具备这门课程的智慧,并希望这里有人能解释我的意思'我做错了,甚至是我应该学习的东西。

我尝试创建一个数组并将所有骰子值分配给该数组,但在比较值时遇到了问题,我不知道该怎么做 dice 1 == dice 2 == dice 3等

然后我尝试为每个骰子制作 5 个数组 1 并使用比较数组方法,我似乎只能让它比较 2 个数组或变量我无法像我一样比较所有 5 个我正在尝试。

public static void main(String[] args) {

    int[] Dicearraytotal1 = new int[10];
    int[] Dicearraytotal2 = new int[10];
    int[] Dicearraytotal3 = new int[10];
    int[] Dicearraytotal4 = new int[10];
    int[] Dicearraytotal5 = new int[10];
    for (int i = 0; i < Dicearraytotal1.length; i++) {
        Integer dice1 = (int) (Math.random() * 6 + 1);
        Integer dice1val = dice1;
        Integer dice2 = (int) (Math.random() * 6 + 1);
        Integer dice2val = dice2;
        Integer dice3 = (int) (Math.random() * 6 + 1);
        Integer dice3val = dice3;
        Integer dice4 = (int) (Math.random() * 6 + 1);
        Integer dice4val = dice4;
        Integer dice5 = (int) (Math.random() * 6 + 1);
        Integer dice5val = dice5;
        Dicearraytotal1[i] = (dice1val);
        Dicearraytotal2[i] = (dice2val);
        Dicearraytotal3[i] = (dice3val);
        Dicearraytotal4[i] = (dice4val);
        Dicearraytotal5[i] = (dice5val);
    Integer total = (dice1val+dice2val+dice3val+dice4val+dice5val);

        System.out.println("Total Of Numbers Generated1: " + Arrays.toString(Dicearraytotal1));
        System.out.println("Total Of Numbers Generated2: " + Arrays.toString(Dicearraytotal2));
        System.out.println("Total Of Numbers Generated3: " + Arrays.toString(Dicearraytotal3));
        System.out.println("Total Of Numbers Generated4: " + Arrays.toString(Dicearraytotal4));
        System.out.println("Total Of Numbers Generated5: " + Arrays.toString(Dicearraytotal5));
        System.out.println("Total: " + total);

【问题讨论】:

  • 您使用Integer而不是int作为骰子值的任何原因?
  • A) 允许您暂时离开课程并学习 Java 命名约定。只有类名使用 UpperCammelCase,变量和字段名使用 camelCase。总是 B) 你在写很多无意义的代码。在第一个循环中使用 两个 临时变量是零意义的。只需分配Dicearraytotal1[i] = (int) Math.random(....) 不要编写不需要编写的代码。看看你想要什么(在每个数组中获取一个值),然后就这样做。

标签: java arrays dice


【解决方案1】:

您缺少一个结束括号来停止for 循环,因此我假设您尝试在循环的每次迭代中打印数组。我对您的代码进行了很多清理,您应该注意其中的一些更改,以便更好地组织您的代码,使其更易于理解。

public static void main(String[] args) {

    int[] diceArrayTotal1 = new int[10];
    int[] diceArrayTotal2 = new int[10];
    int[] diceArrayTotal3 = new int[10];
    int[] diceArrayTotal4 = new int[10];
    int[] diceArrayTotal5 = new int[10];
    int[] totals = new int[10];

    for (int i = 0; i < diceArrayTotal1.length; i++) {
        diceArrayTotal1[i] = (int) (Math.random() * 6 + 1);
        diceArrayTotal2[i] = (int) (Math.random() * 6 + 1);
        diceArrayTotal3[i] = (int) (Math.random() * 6 + 1);
        diceArrayTotal4[i] = (int) (Math.random() * 6 + 1);
        diceArrayTotal5[i] = (int) (Math.random() * 6 + 1);
        totals[i] = (diceArrayTotal1[i] + diceArrayTotal2[i] + diceArrayTotal3[i] + diceArrayTotal4[i] + diceArrayTotal5[i]);
    }
        System.out.println("Total Of Numbers Generated1: " + Arrays.toString(diceArrayTotal1));
        System.out.println("Total Of Numbers Generated2: " + Arrays.toString(diceArrayTotal2));
        System.out.println("Total Of Numbers Generated3: " + Arrays.toString(diceArrayTotal3));
        System.out.println("Total Of Numbers Generated4: " + Arrays.toString(diceArrayTotal4));
        System.out.println("Total Of Numbers Generated5: " + Arrays.toString(diceArrayTotal5));
        System.out.println("Totals: " + Arrays.toString(totals));
}   

此外,我添加了一个 totals 数组,该数组保留每个索引的总数,而不是像您所做的那样在每个循环中打印它。您没有添加比较代码,因此我无法为您提供帮助。如果您需要对任何更改进行任何说明,请告诉我。我运行了这段代码,它成功生成了你需要的数组和总数。

【讨论】:

  • 非常感谢,我真的很感激,我正在做笔记,我可以看到你对数组总计所做的更改我还没有整理出比较代码。非常感谢:)
【解决方案2】:
public static void main(String[] args) {
    int[] results = new int[6]; // This array will hold the number of time each dice was rolled, so for example results[0] is how many 1 s you have results[5] is how many 6 you have
    Random random = new Random();
    for (int i = 0; i < 5; i++) {   // roll the dice 5 times
        results[random.nextInt(6)]++;   //increase the counter of the appropriate value
    }

    boolean balut = false;
    for (int i = 0; i < 6; i++) {
        System.out.println("Number of " + (i+1) + ": " + results[i]);
        if (results[i] == 5) {
            balut = true;
        }
    }

    if (balut) {
        System.out.println("Balut!");
    }
}

这里我只实现了对 Balut 的检查,但重点是我如何计算骰子结果。希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    • 2012-02-29
    • 1970-01-01
    • 2021-12-14
    • 2015-08-25
    • 2015-12-24
    相关资源
    最近更新 更多