下面的代碼模擬骰子,我對兩個骰子匹配的次數感興趣,並且想法是首先打印出0和1,以顯示出現這種情況的頻率。然後打印數組,其中應包含什麼時候獲得「double」,例如:第一個包含3,這是第一個double之前的拋出數...用戶給出的限制45說會結束程序。錯誤的數組輸出,java
public static void main(String[] args) {
int limiteDoublons = Integer.parseInt(JOptionPane.showInputDialog("Nombre lancer: ").trim());
int[] t = new int[limiteDoublons];
int d1 = 0;
int d2 = 0;
int nbDoublons = 0;
int nbLancer = 0;
do {
for(int x = 0; x<limiteDoublons;x++){
//shuffle
d1 = (int)(Math.random() * 7) + 1;
d2 = (int)(Math.random() * 7) + 1;
//si doublon print 1 et augmente le nbDoublon
//donne la valeur du tableau suivante ;a valeur de nblancer
if(d1 == d2) {
System.out.print("1");
nbDoublons++;
t[x] = nbLancer;
}
else {
System.out.print("0");
}
if ((nbLancer + 1) % 20 == 0) {
System.out.println();
}
nbLancer++;
}
} while (nbDoublons < limiteDoublons);
System.out.println("\nOrdre des boulons: ");
Arrays.sort(t);
for (int i = 0; i < limiteDoublons; i++) {
System.out.printf("%1$4d", t[i]);
if ((i + 1) % 10 == 0)
System.out.println();
}
}
}
輸出:
10001101100000000000
01001100000000000000
00000000000000000000
00101100010100110010
00000000000000100000
00001100010010000000
00000000100000000000
00001000000000000000
01000100000011000101
00000010001000000100
00000001000000000100
01010000000000100001
01100100100000001000
10000000001010010000
00001000000000000000
000000000100000
Ordre des boulons:
0 0 0 0 0 0 0 0 0 0
0 7 8 21 25 69 74 78 94 105
109 112 161 165 173 177 179 186 190 207
217 221 223 234 241 242 245 248 256 260
270 272 275 284 309
出人意料的是下面的 「勳章DES boulons」 0。它沒有任何意義,我會得到我的第一雙在投0等等...
的可能的複製[陣列的問題,也解決不了它的Java(http://stackoverflow.com/questions/40412988/array-issue-cant-solve-it-java ) – shmosel
是的你是對的,我再次問它,但它更詳細,我試圖得到答案,任何人都可以給我一個嗎? – NewBie1234
爲什麼所有的零都是?因爲當隨機結果不匹配時,你沒有給數組t分配任何東西。 –