这个问题已经在这里有了答案:

我试图将随机值添加到数组。 用户必须说出多少个值(长度)并给出minval和maxval(ex minval = 3表示没有值低于3)。 这就是我得到的:

int[] GetallenArray;

public IntegerArray(int length, int maxval, int minval) {

    this.GetallenArray = new int[length];
    for (int i = 0; i < GetallenArray.length; i++)
    {
        this.GetallenArray[i] = // Random values between the maxval and minval
    }
}

===============>>#1 票数:2

在循环外实例化一个Random对象

Random random = new Random();

然后在里面循环

this.GetallenArray[i] = random.nextInt((maxval - minval)+1) + minval;

===============>>#2 票数:1

Random random = new Random();
this.GetallenArray = new int[length];
  for (int i = 0; i < GetallenArray.length; i++){
    this.GetallenArray[i] = random.nextInt(50) + 1; 
   //50 is the maximum and the 1 is our minimum 
  }
}

  ask by Mathias Verhoeven translate from so

未解决问题?本站智能推荐: