這個java程序生成一個1-6之間的隨機數。它還允許用戶選擇在1-6之間生成多少個隨機數。我現在需要記錄並輸出每個數字出現多少個實例。即。每個數字1,2,3,4,5,6在程序的一次運行中出現多少次。我會如何去做這件事?這是我到目前爲止。如何計算這個Java程序中的值的數量?
import java.util.Random;
public class DiceRandomness {
public static void main(String[] args) {
Random generator = new Random();
System.out.print("How many throws of the dice: ");
int num = Console.readInt();
int[] Results = new int[10000];
for (int i = 0; i < num; i++) {
int d = 1 + generator.nextInt(6);
System.out.println(d + " ");
}
}
}
我會創建一個6個整數的數組,並增加對應於所選號碼的數組條目。 –
或7,以避免一個 - 只使用計數器[結果]而不是計數器[結果-1] –