2015-03-24 94 views
-1

當我寫System.out.print(dice)輸出隨機生成的數字是這樣的:如何使用數組來創建多個輸出?

[3|4|7|6|6] 

如何我可以使用陣列有多個值嗎?

/** 
* This program rolls Yahtzee! dice 1,000,000 times, recording the number of 
* Yahtzees which occur. After the trials statistics are produced. 
*/ 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    // TODO code your application logic here 
    Scanner input = new Scanner(System.in); 
    Random generator = new Random(); 
    YahtzeeDice dice = new YahtzeeDice(generator); 
    System.out.print(dice); 
    } 
} 
+0

在這裏檢查或多或少相同的問題。 http://stackoverflow.com/questions/1067073/initialising-a-multidimensional-array-in-java – 2015-03-24 23:57:24

回答

0

沒有足夠的評論聲望。我不完全確定你需要什麼。 您目前的代碼是否從一次運行中生成這5個數字?你的YahzeeDice課程是什麼? 如果你想骰子的值記錄到一個數組,你可以簡單的把在一個循環,並不斷增加骰子數組:

public List<YahtzeeDice> generate(int n) { 
    List<YahtzeeDice> results = new ArrayList<YahtzeeDice>(); 
    YahtzeeDice dice; 
    for(int i = 0; i < n; i++) { 
     dice = new YahtzeeDice(new Random()); 
     results.add(dice); 
    } 
    return results; 
} 

像這樣的事情?如果這個編譯沒有測試過。 然後你需要從你的主叫:

List<YahtzeeDice> diceResults = generate(1000000); 
相關問題