调用类java.lang.Math的成员方法“public static double random()”运算下面表达式10000次,(int)(Math.random(0*20+0.5)统计其中生成

调用类java.lang.Math的成员方法“public static double random()”运算下面表达式10000次,(int)(Math.random(0*20+0.5)统计其中生成的整数0,1,2,.,20的个数分别是多少,并输出统计结果。

package com.atrrx.java;

public class Test {
    public static void main(String[] args) {
        int count[]=new int[21];
        for (int i = 0; i < 10000; i++) {
            int num= (int)Test.random();
            count[num]++;
        }
        for (int i = 0; i < 21; i++) {
            System.out.println(i+":"+count[i]+"个");
        }
    }
    public static double random(){
        return (Math.random()*20+0.5);
    }
}

在代码中,我们先创建一个长度为21的数组count,用于统计生成的整数0到20的个
数。然后使用for循环运算下面表达式10000次,每次调用Math.random0方法生成一个随机数,将其乘以20并加上0.5后转换为整型数,并将该整数对应位置的count数组值加1。
最后,再使用for循环遍历count数组,输出生成的0到20之间的整数的个数统计结果。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值