掷6面骰子6000次每个点数出现的概率
掷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); } }
结果:
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接
赞 (0)