我已经编写了一个带有子类的应用程序,该应用程序将根据用户输入的几个方面掷骰子,并使用一个整数将骰子掷出一定数量的次数。<<<<<<<<<<<<<<<<<<<<<<<</p>
例如: 用户进入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.
}
}
}
然后,计算器将根据程序ran ran ran ran 结果的次数运行和输出百分比,因此...
之类的东西所需的输出:
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'for'for'现在是重复'sum',但是很多时候骰子滚动。总和也不会返回每个迭代的数字。
我的主要目标是将用户要求的任何次数掷骰子。使用Java数组存储结果。例如,每次我要执行下一个掷骰时,都会创建一个新的骰子。这样,我可以将每对骰子存储在一个数组中。
我正在尝试学习如何用零件编写编码,但是我现在迷路了,以至于我感到被击败了。不能为您提供的任何指导,不能感谢您。如果这根本令人困惑,我真的很抱歉,我不完全理解这些说明...
您可以使用2D数组或哈希地图来执行此操作,但我更喜欢2x arralist
NumberFormat formatter = new DecimalFormat("#0.00");
ArrayList<Integer> numbers = new ArrayList<Integer>();
ArrayList<Integer> counts = new ArrayList<Integer>();
int maxNum = DiceRolling.diceSides;
Scanner sc = new Scanner(System.in);
int rollnum;
int randomValue;
System.out.println("Welcome to the Dice Roll Stats Calculator!");
System.out.println("Enter amount of rolls: ");
rollnum = sc.nextInt();
for (int i = 0; i < rollnum; i++) {
randomValue = (1 + (int) (Math.random() * maxNum)) + (1 + (int) (Math.random() * maxNum));
if(numbers.contains(randomValue)){
int position = numbers.indexOf(randomValue);
counts.set(position, counts.get(position)+1);
}else{
numbers.add(randomValue);
counts.add(1);
}
}
System.out.println("TotaltCounttPercentage");
System.out.println("-----t---------t----------");
for(int i = 0; i<numbers.size(); i++){
System.out.println(numbers.get(i) +"t" + counts.get(i) + "t" + formatter.format(((double)(counts.get(i)*100))/rollnum) + "%";
}
是您的答案吗?我希望这有效。