0

Math.random機能をテストしているという簡単な質問があります。(int) (Math.random()*6)+1100個のボックスのそれぞれの結果を配列に割り当てて値を格納しようとしています。しかし、それがステートメントではないというエラーが発生します。誰かがおそらくいくつかのガイダンスを提供できますか?

public shoes(int[] pairs)
{
   System.out.println("We will roll go through the boxes 100 times and store the input for the variables");
   for(int i=0; i < 100; i++)
   {
       //("Random Number ["+ (i+1) + "] : " + (int)(Math.random()*6));
       int boxCount[i]  =  (int) (Math.random()*6) + 1;
   }
}
4

2 に答える 2

5

構文エラーがあります。boxCountインスタンス化されておらず、既知のタイプではありません。boxCountアレイを使用する前に、アレイを作成する必要があります。以下の例を参照してください。

public void shoes(int[] pairs)
{
    System.out.println("We will roll go through the boxes 100 times and store the input for the variables");
    int boxCount[] = new int[100];
    for(int i=0; i < boxCount.length; i++)
    {
        //("Random Number ["+ (i+1) + "] : " + (int)(Math.random()*6));
        boxCount[i]  =  (int) (Math.random()*6) + 1;
    }
}
于 2012-06-20T19:23:20.797 に答える
3
int boxCount[i]  =  (int) (Math.random()*6) + 1;

このコード行は、配列などを作成しようとしているように見えます。

boxCountforループの前に配列を作成したい:

int boxCount[] = new int[100];

次に、ループでこれを行うことができます。

boxCount[i]  =  (int) (Math.random()*6) + 1;
于 2012-06-20T19:23:48.793 に答える