【发布时间】:2017-12-11 11:19:39
【问题描述】:
我编写了一个应用程序,它有一个子类,它会根据用户输入的面数来掷骰子,并使用一个整数来掷骰子一定次数。
如: 用户输入6面,想掷骰子1000次。
我也应该使用与我编码的数组类似的数组。
我目前拥有的:
public class DDiceRoller {
public static void diceStats() {
int maxNum = DiceRolling.diceSides;
Scanner sc = new Scanner(System.in);
int randomValue = 1 + (int) (Math.random() * maxNum);
int randomValue2 = 1 + (int) (Math.random() * maxNum);
int die1 = (randomValue);
int die2 = (randomValue2);
int sum = die1 + die2;
int rollnum;
int idx;
System.out.println("Welcome to the Dice Roll Stats Calculator!");
int[] combinations = new int[maxNum]; //I haven't even used this variable yet, don't know how.
System.out.println("Enter amount of rolls: ");
rollnum = sc.nextInt();
for (idx = 0; idx < rollnum; idx++) {
System.out.println(sum); //I know this is wrong, just don't know what to do.
}
}
}
然后计算器将运行程序并根据程序运行的次数输出百分比哪些结果......所以类似于......
所需的输出:
Total Count Percentage
----- --------- ----------
2 123 3.01%
3 456 6.07%
4 etc 3.19%
5 ??? 4.45%
6 ??? 8.90%
7 ??? 8.62%
8 ??? 7.63%
9 ??? 6.92%
10 ??? 5.40%
11 ??? 6.96%
12 ??? 8.36%
当前输出: 现在我得到的只是输入的相同值,因为我现在对我的“for”循环所做的只是重复“sum”,无论多少次骰子滚动。总和也不会为每次迭代返回不同的数字。
我的主要目标是按用户要求的任意次数掷骰子。使用 Java 数组来存储结果。例如,每次我要执行下一个掷骰时,创建一对新骰子。这样,我可以将每对骰子存储在一个数组中。
我正在尝试学习如何分段编写代码,但我现在迷失了方向,感觉被打败了。不能感谢您您可以提供任何指导。 如果这令人困惑,我真的很抱歉,我不完全理解说明......
【问题讨论】:
-
你能展示你想要做什么吗?