public class SpringBootDemo31Application { /** * 一等奖:比例(percentOne)10/100 * 二等奖:比例(percentTwo)20/100 * 三等奖:比例(percentThree)30/100 * 四等奖:比例(percentFour)40/100 * * @param args */ public static void main(String[] args) { String percentOne="10/100"; String percentTwo="20/100"; String percentThree="30/100"; String percentFour="40/100"; int weightOne=getWeight(percentOne); int weightTwo=getWeight(percentTwo); int weightThree=getWeight(percentThree); int weightFour=getWeight(percentFour); int weightTotal=weightOne+weightTwo+weightThree+weightFour; double b1=(double) weightOne/weightTotal; double b2=b1+(double) weightTwo/weightTotal; double b3=b2+(double) weightThree/weightTotal; double b4=b3+(double) weightFour/weightTotal; int i1=0,i2=0,i3=0,i4=0,i5=0; for(int t=0;t<1000;t++) { double randomN = Math.random(); String result = "恭喜您获得"; if (randomN <= b1) { result += "一等奖"; i1++; } else if (b1 < randomN && randomN <= b2) { result += "二等奖"; i2++; } else if (b2 < randomN && randomN <= b3) { result += "三等奖"; i3++; } else if (b3 < randomN && randomN <= b4) { result += "四等奖"; i4++; } else { result = "没有中奖"; i5++; } //System.out.println(result); } System.out.println(i1+" "+i2+" "+i3+" "+i4+" "+i5); System.out.println((double)i1/(i1+i2+i3+i4)); System.out.println((double)i2/(i1+i2+i3+i4)); System.out.println((double)i3/(i1+i2+i3+i4)); System.out.println((double)i4/(i1+i2+i3+i4)); } public static int getWeight(String percent){ String[] splits=percent.split("/"); return Integer.parseInt(splits[0]); }
java 抽奖
最新推荐文章于 2023-02-04 16:20:42 发布