阵列从1-300 30产生随机数的返回全0,而不是其他数字 [英] Array to generate 30 random numbers from 1-300 returns all 0s instead of other numbers

查看:137
本文介绍了阵列从1-300 30产生随机数的返回全0,而不是其他数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在那里我试图从1-300的范围只是一个主要方法填充30随机整数数组的问题,多了一个方法称为负载()。出于某种原因,我下面的code产生与我的测试语句中的数组的System.out.println(这是30的随机数:+ Arrays.toString(randomThirty)); 但它充满了只有在各个领域的0,没有其他号码。

I have a problem where I'm trying to fill an array with 30 random integers from the range 1-300 with just a main method and one more method called LOAD(). For some reason my code below generates an array with my test statement System.out.println("Here are 30 random numbers: " + Arrays.toString(randomThirty)); but it's filled with only 0s and no other numbers in all fields.

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); 
}
}

}

推荐答案

的原因是你打印出数组第一和第二填充它。

The reason is that you print out the array first, and populate it second.

在换句话说, LOAD()不会被调用,直到您已经打印出数组。

In other words, LOAD() doesn't get called until after you've printed out the array.

这篇关于阵列从1-300 30产生随机数的返回全0,而不是其他数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆