package
test;
import
java.util.HashMap;
import
java.util.Map;
public
class
MathTest
{
/**
*
编写一个程序:调用java.lang.Math的成员函数“public
static
double
random()”
*
运算下面表达式10000次,
*
(int)
(Math.random()*20+0.5)
*
统计其中生成的整数0、1、2、……、20的个数分别是多少
*
,并以出现次数多少排序后(由多到少排序)输出统计结果。
*
*/
public
static
void
main(String[]
args)
{
Map
intMap
=
new
HashMap
for
(int
i
=
0;
i
<=
20;
i++)
{
intMap.put(i,
0);
}
int
temp;
int
i;
int
mapValue;
for
(i=0;
i
<
10000;
i++)
{
temp
=
(int)
(Math.random()*20+0.5);
mapValue
=
(int)
intMap.get(temp);
mapValue++;
intMap.put(temp,
mapValue);
}
for
(int
j
=
0;
j
<
intMap.size();
j++)
{
System.out.println(j+"出现了:"+intMap.get(j)+"次");
}
}
}