掷6面骰子6000次每个点数出现的概率
时间:2020-10-05 21:31:36|栏目:JAVA代码|点击: 次
掷6面骰子6000次每个点数出现的概率
import java.util.Random; public class Statistics { final static int Maxsize = 6000; public static void main(String[] args) { // TODO Auto-generated method stub Random rand = new Random(); int temp[] = new int[Maxsize]; for(int i = 0; i < Maxsize; i++) temp[i] = rand.nextInt(6) + 1; int a=0, b=0, c=0, d=0, e=0, f=0; for(int i = 0; i < temp.length; i++) { if(temp[i] == 1) a++; else if(temp[i] == 2) b++; else if(temp[i] == 3) c++; else if(temp[i] == 4) d++; else if(temp[i] == 5) e++; else if(temp[i] == 6) f++; } System.out.println("1出现:" + a + " 2出现:" + b + " 3出现:" + c + " 4出现:" + d + " 5出现:" + e + " 6出现:" + f + "\n"); float one = (float)a/Maxsize, two = (float)b/Maxsize, three = (float)c/Maxsize, four = (float)d/Maxsize; float five = (float)e/Maxsize, six = (float)f/Maxsize; System.out.println(1 + "出现的概率是:" + one); System.out.println(2 + "出现的概率是:" + two); System.out.println(3 + "出现的概率是:" + three); System.out.println(4 + "出现的概率是:" + four); System.out.println(5 + "出现的概率是:" + five); System.out.println(6 + "出现的概率是:" + six); } }
结果:
总结
上一篇:postman测试post请求参数为json类型的实例讲解
栏 目:JAVA代码
本文标题:掷6面骰子6000次每个点数出现的概率
本文地址:http://www.codeinn.net/misctech/6988.html
阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机