掷6面骰子6000次每个点数出现的概率
程序员文章站
2024-02-17 23:29:58
掷6面骰子6000次每个点数出现的概率
import java.util.random;
public class statistics {
final s...
掷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); } }
结果:
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
推荐阅读