想知道这里应该怎么把随机数存进去?数组中的个数是键盘输入进去,根据元素个数产生随机数然后存入数组,这里不清楚具体怎么做?

java怎么产生随机个数并存入到数组中?
想知道这里应该怎么把随机数存进去?数组中的个数是键盘输入进去,根据元素个数产生随机数然后存入数组,这里不清楚具体怎么做?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
关注
math.random() 产生随机数,然后保存到数组,跟你输入是一样的呀
public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入产生随机数个数:"); int count =scan.nextInt(); int a[]=new int [count]; for(int i=0;i<count;i++){ a[i]=(int) (Math.random() * 100); } Arrays.sort(a); System.out.print("倒叙结果是"+"\t"); for(int i=count-1;i>=0;i--){ System.out.print(a[i]+"\t"); } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 2无用