2017-02-15 49 views
-3

我已經返回了一個作業分配的骰子卷,並且在計算每個數字滾動的次數方面遇到了問題。我返回到主要卷並已將其添加到數組,但它只是不計算。我嘗試了人們所說的話,做出了一些改變,更接近了,但仍然沒有得到正確的答案。返回不計數在櫃檯

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 
+2

你的程序不使用任何陣列。您可以考慮查看關於Java數組的教程:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html –

+0

返回* what * array? –

回答

0

你確實有,讓你想擲骰量的用戶的輸入。請在for循環中使用該數字,然後創建一個整數大小爲14的數組。在每個循環中,您只需向數組[數字滾動]加1,最後循環遍歷數組中的每個項目,就像代碼中的那樣,並打印該值。

我希望這可以幫助你。

0

首先,您的代碼中沒有任何數組。每次得到「7」結果的原因是因爲「整數滾動」在之前被定義爲,而不是循環內部。

嘗試更換 '輥' 與 'simDiceRoll(烏塞林)':

System.out.println(i + " was rolled " + simDiceRoll(userIn) + " times");