java中带参带返回值的方法的调用

Rainbow___Girl 2016-09-10 10:55:36
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)

...全文
955 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
e824595097 2016-09-20
  • 打赏
  • 举报
回复
增强for不可以这么用,你的数据根本就无法添加进去,需要用正常的for循环
大__淼 2016-09-19
  • 打赏
  • 举报
回复
你这代码 能编译通过????
大W小w 2016-09-10
  • 打赏
  • 举报
回复
我虽然是菜鸟,但这东西我应该也是一两分钟的事情,今天居然在这弄了半个小时,真是醉了
大W小w 2016-09-10
  • 打赏
  • 举报
回复
foreach注释里面 nums[num] = (int)( Math.random()*100); 这句错了,应该是 num = (int)( Math.random()*100);
Rainbow___Girl 2016-09-10
  • 打赏
  • 举报
回复
引用 14 楼 SmallYoung_H 的回复:
看着应该是没错了,好吧!我承认我也是菜鸟


把foreach改成 for循环,结果就正常了

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(9);

// 将数组转换为字符串并输出
System.out.println(Arrays.toString(age));
}
/*
* 功能:创建指定长度的int型数组,并生成100以内随机数为数组中的每个元素赋值
* 定义一个带参带返回值的方法,通过参数传入数组的长度,返回赋值后的数组
*/
public int[] getArray(int length) {
// 定义指定长度的整型数组
int[] nums = new int[length];

// 循环遍历数组赋值
//for ( int num: nums ) {
for(int i = 0;i < nums.length;i++) {
// 产生一个100以内的随机数,并赋值给数组的每个成员
nums[i] = (int)(Math.random()*100);
}
return nums; // 返回赋值后的数组
}
}

结果:[65, 60, 72, 52, 4, 95, 95, 71, 42]
大W小w 2016-09-10
  • 打赏
  • 举报
回复
foreach应该是可以的呀
Rainbow___Girl 2016-09-10
  • 打赏
  • 举报
回复
引用 11 楼 yuxiangaaaaa 的回复:
你的代码本身就报错了,还能编译吗?
public int[] getArray(int length) {
		// 定义指定长度的整型数组
		int[] nums = new int[length];

		// 循环遍历数组赋值
		//for (int num : nums) {

			// 产生一个100以内的随机数,并赋值给数组的每个成员
			//nums[num] = (int)( Math.random()*100);
		//}
		for (int n = 0;n<nums.length;n++) {

			// 产生一个100以内的随机数,并赋值给数组的每个成员
			nums[n] = (int) (Math.random()*100);
		}
		return nums; // 返回赋值后的数组
	}
本来想用 foreach循环呢,结果运行结果不对。改成for循环就可以正常运行了
大W小w 2016-09-10
  • 打赏
  • 举报
回复
看着应该是没错了,好吧!我承认我也是菜鸟
大W小w 2016-09-10
  • 打赏
  • 举报
回复
看漏了,没看到下面的
大W小w 2016-09-10
  • 打赏
  • 举报
回复
如果要100以内的数可以在random方法后乘以100
自由自在_Yu 2016-09-10
  • 打赏
  • 举报
回复
你的代码本身就报错了,还能编译吗?
public int[] getArray(int length) {
		// 定义指定长度的整型数组
		int[] nums = new int[length];

		// 循环遍历数组赋值
		//for (int num : nums) {

			// 产生一个100以内的随机数,并赋值给数组的每个成员
			//nums[num] = (int)( Math.random()*100);
		//}
		for (int n = 0;n<nums.length;n++) {

			// 产生一个100以内的随机数,并赋值给数组的每个成员
			nums[n] = (int) (Math.random()*100);
		}
		return nums; // 返回赋值后的数组
	}
大W小w 2016-09-10
  • 打赏
  • 举报
回复
random方法的返回值就是0到1之间的
Rainbow___Girl 2016-09-10
  • 打赏
  • 举报
回复
引用 7 楼 SmallYoung_H 的回复:
因为随机出来的数是在0到1之间的小数
可我写的是100以内的数呀,为什么出来的都是0.应该是有哪里不对,可是找不出来
大W小w 2016-09-10
  • 打赏
  • 举报
回复
我好像出了个错,random的参数可以不要
大W小w 2016-09-10
  • 打赏
  • 举报
回复
因为随机出来的数是在0到1之间的小数
Rainbow___Girl 2016-09-10
  • 打赏
  • 举报
回复
引用 4 楼 u011299745 的回复:
nums = Math.random(nums); 这一句两个nums都换成num。 你都遍历了怎么不用当前的元素而且用整个数组。
明白了。不过Math.random() 是无参的
Rainbow___Girl 2016-09-10
  • 打赏
  • 举报
回复
引用 2 楼 SmallYoung_H 的回复:
random方法参数给一个数组对象好像也不对
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(9); // 将数组转换为字符串并输出 System.out.println(Arrays.toString(age)); } /* * 功能:创建指定长度的int型数组,并生成100以内随机数为数组中的每个元素赋值 * 定义一个带参带返回值的方法,通过参数传入数组的长度,返回赋值后的数组 */ public int[] getArray(int length) { // 定义指定长度的整型数组 int[] nums = new int[length]; // 循环遍历数组赋值 for ( int num: nums ) { // 产生一个100以内的随机数,并赋值给数组的每个成员 num = (int)(Math.random()*100); } return nums; // 返回赋值后的数组 } } 随机数是double类型,所以需要强转成int,赋值给成员 num 而不是数组nums. 不过运行结果是 [0, 0, 0, 0, 0, 0, 0, 0, 0] 不是其他数。不知道现在这程序对不对???
绿树苍天 2016-09-10
  • 打赏
  • 举报
回复
nums = Math.random(nums); 这一句两个nums都换成num。 你都遍历了怎么不用当前的元素而且用整个数组。
大W小w 2016-09-10
  • 打赏
  • 举报
回复
准确来说,那一句应该是: num = Math.Random(nums.length); length具体是方法还是属性,这个忘了
大W小w 2016-09-10
  • 打赏
  • 举报
回复
random方法参数给一个数组对象好像也不对
加载更多回复(2)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧