我有一个问题,我正在尝试使用一个主方法和一个名为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);
}
}
}
答案 0 :(得分:12)
原因是您首先打印出阵列,然后再填充它。
换句话说,{<1}}在您打印出数组后 后才会被调用。
答案 1 :(得分:1)
你在调用方法之前打印....改变调用的顺序,它会起作用