【发布时间】: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(....)不要编写不需要编写的代码。看看你想要什么(在每个数组中获取一个值),然后就这样做。