java 抽奖

public class SpringBootDemo31Application {

   /**
    * 一等奖:比例(percentOne10/100
    * 二等奖:比例(percentTwo20/100
    * 三等奖:比例(percentThree30/100
    * 四等奖:比例(percentFour40/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]);
   }
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值