java中带参带返回值的方法的调用
package com.imooc4;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
// 创建对象,对象名为hello
Test hello = new Test();
// 调用方法并将返回值保存在变量中
int[] age = hello.getArray(8);
// 将数组转换为字符串并输出
System.out.println(Arrays.toString(age));
}
/*
* 功能:创建指定长度的int型数组,并生成100以内随机数为数组中的每个元素赋值
* 定义一个带参带返回值的方法,通过参数传入数组的长度,返回赋值后的数组
*/
public int[] getArray(int length) {
// 定义指定长度的整型数组
int[] nums = new int[length];
// 循环遍历数组赋值
for ( int num: nums ) {
// 产生一个100以内的随机数,并赋值给数组的每个成员
nums = Math.random(nums);
}
return nums; // 返回赋值后的数组
}
}
请问大神们,这是为什么报错?
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method random() in the type Math is not applicable for the arguments (int[])
at com.imooc4.Test.getArray(Test.java:30)
at com.imooc4.Test.main(Test.java:12)