Math.random()生成一维随机数组

 

 

//一维随机数组

public class RandomArray
{
    public static void main(String[] args)
    {
        int[] nums = new int[(int) (Math.random() * 10)];

        System.out.println("length = " + nums.length); //输出随机数组的长度

        for (int i = 0; i < nums.length; i++)
        {
            nums[i] = (int) (Math.random() * 100)  - (int) (Math.random() * 10);
        }

        // for (int i = 0; i < nums.length; i++)
        //     System.out.println(nums[i]);

        //或者:
        for (int item : nums)
            System.out.println(item);
    }
}

 

posted @ 2020-05-25 22:08  profesor  阅读(785)  评论(0编辑  收藏  举报