当前位置: 代码迷 >> Java相关 >> [求助]请教Math.random()的使用
  详细解决方案

[求助]请教Math.random()的使用

热度:1043   发布时间:2007-06-04 19:25:31.0
[求助]请教Math.random()的使用
1、使用Math.random()方法产生50个0―100的随机整数,并将它们存放到一维数组score中,然后统计各分数段上的次数(即分别在0--9,10--19,20--29,…,80―89,90―100上的次数),并将分类统计结果存入到数组result中,最后输出数组result各元素的值。
--------------------------
请问怎么实现这道题目~~~~~~~~~~~~
搜索更多相关的解决方案: random  Math  result  

----------------解决方案--------------------------------------------------------
可以用switch语句统计各分数段上的次数并将分类统计结果存入到数组result中
用for输出数组result各元素的值
具体细节自己做吧!~
----------------解决方案--------------------------------------------------------
import java.util.*;
class Two
{
public static void main(String args[])
{
Random num=new Random();
int[] result={0,0,0,0,0,0,0,0,0,0};
int[] score=new int[50];
for(int i=0;i<50;i++)
{
score[i]=num.nextInt(100);
if(score[i]>90)
result[9]++;
……………………
}
for(int j=0;j<9;j++)
System.out.println(result[j]);
}
}
省略的可以自己做了吧~~~~
----------------解决方案--------------------------------------------------------
回复:(非与飞)[求助]请教Math.random()的使用
程序代码:
score[i]=(int)Math.random()*101;

[此贴子已经被作者于2007-6-5 15:16:31编辑过]



----------------解决方案--------------------------------------------------------
Math.random()产生的是0-1的随机数
把他*100不就是0-100的了吗?!
----------------解决方案--------------------------------------------------------
可是楼主要的是0-100的整数?100*Math.random()不行的,3楼的行,用java.util.Random.nextInt(100)方法!
----------------解决方案--------------------------------------------------------
也可以强制转换啊!
(int)(100*Math.random())
----------------解决方案--------------------------------------------------------
  相关解决方案