从1-300生成30个随机数的数组返回全0而不是其他数字

art*_*emd 1 java arrays random

我有一个问题,我试图用一个主方法和一个名为LOAD()的方法来填充1-300范围内的30个随机整数.出于某种原因,我的代码生成了一个包含我的测试语句的数组,System.out.println("Here are 30 random numbers: " + Arrays.toString(randomThirty));但它只填充了0并且在所有字段中都没有其他数字.

import java.util.*;

public class randArray
{

public static void main(String args[])
{
     //main method which creates an array to store 30 integers

    int[] randomThirty = new int[30]; //declare and set the array randomThirty to have 30 elements as int type

    System.out.println("Here are 30 random numbers: " + Arrays.toString(randomThirty));

    LOAD(randomThirty); // the job of this method is to populate this array with 30 integers in the range of 1 through 300.

}

public static void LOAD(int[] randomThirty)
{
    // looping through to assign random values from 1 - 300
    for (int i = 0; i < randomThirty.length; i++) {
        randomThirty[i] = (int)(Math.random() * 301); 
}
}
Run Code Online (Sandbox Code Playgroud)

}

NPE*_*NPE 12

原因是您先打印出阵列,然后再填充它.

换句话说,LOAD()直到打印出阵列才会调用.