时间: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); } }
结果:
总结