私は指定された量のロール(num;
)のサイコロをシミュレートし、その結果を印刷するプログラム(以下のコード)を持っています。 何らかの理由で、プログラムが完了できるロールの最大量は2,147,483,583であり、intの上限は2,147,483,647ではありません。ロールの数として2,147,483,647が入力された場合、プログラムは決して出力を生成しません。forループの結果が無限ループで2,147,483,647になるのはなぜですか?
具体的な理由はありますか?
import java.security.SecureRandom;
public class RollDie {
// number of rolls
private static float num = 2147483583;
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
System.out.print("Rolling...\n");
SecureRandom randomNumbers = new SecureRandom();
int frequency1 = 0;
int frequency2 = 0;
int frequency3 = 0;
int frequency4 = 0;
int frequency5 = 0;
int frequency6 = 0;
for (int roll = 1; roll <= num; roll++) {
// randomly selecting face ('rolling')
int face = 1 + randomNumbers.nextInt(6);
switch (face) {
case 1:
++frequency1;
break;
case 2:
++frequency2;
break;
case 3:
++frequency3;
break;
case 4:
++frequency4;
break;
case 5:
++frequency5;
break;
case 6:
++frequency6;
break;
}
}
long end = System.currentTimeMillis();
long totalTime = ((end - start)/1000);
// displaying results
System.out.println("\nFace\tFrequency");
System.out.printf("1\t%d%n2\t%d%n3\t%d%n4\t%d%n5\t%d%n6\t%d%n",
frequency1, frequency2, frequency3, frequency4, frequency5,
frequency6);
System.out.println("\nTime taken: " + totalTime + " seconds.");
}
}
ここで何が起こっているのか把握するために少しデバッグし、より具体的な質問(または2つ)を聞いた場合、この質問にはもっと大きな将来価値があったはずです。 – Dukeling
私はそれを改善するために私の投稿を編集する方法についての任意の提案? @Dukeling –
問題は、この質問の理想的なバージョンに到達するためには、あなたの質問に根本的な変更が必要だが、この段階では、回答の一部が無効になります。具体的には、ここでは2つの問題があります:(1)floatを使って大きな整数を表現しようとする、(2)最大2,147,483,647にループしようとする - 理想的には2つの別々の質問であるべきです。あなたは今それを本当に絞り込むことはできません。しかし、あなたができることは[できるだけコードを減らす](https://ideone.com/CVZ0Gr)です。 – Dukeling