我已經返回了一個作業分配的骰子卷,並且在計算每個數字滾動的次數方面遇到了問題。我返回到主要卷並已將其添加到數組,但它只是不計算。我嘗試了人們所說的話,做出了一些改變,更接近了,但仍然沒有得到正確的答案。返回不計數在櫃檯
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How many times should I roll the dice? ");
int userIn = input.nextInt();
int[] rolls = new int[13];
rolls[simDiceRoll(userIn)]++;
for(int i = 2; i < rolls.length; i++) {
System.out.println(i + " was rolled " + simDiceRoll(userIn) + " times");
}
}
public static int simDiceRoll (int userNum) {
int roll1 = (int) (Math.random() * 6) + 1;
int roll2 = (int) (Math.random() * 6) + 1;
int rolls = roll1 + roll2;
return rolls;
}
輸出
How many times should I roll the dice? 10000
2 was rolled 10 times
3 was rolled 10 times
4 was rolled 7 times
5 was rolled 5 times
6 was rolled 8 times
7 was rolled 12 times
8 was rolled 9 times
9 was rolled 6 times
10 was rolled 10 times
11 was rolled 6 times
12 was rolled 6 times
你的程序不使用任何陣列。您可以考慮查看關於Java數組的教程:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html –
返回* what * array? –