zoukankan      html  css  js  c++  java
  • 微信红包的实现(修改后)

    封装一个微信用包类,主要的功能有|:

    红包中存有钱,多少个人分;每个分到的钱数,及获收红包的时间;

    主要的功能有

                        打开红包;(如果打开红包的人数没有达到上限,则可以打开,并随机获得金额)

                        查看红包分配信息;

                        设置截止日期;(如果超过最大金额,则再打时提示过期)

    思路:微信红包类应该具有的属性有红包金额,红包数,过期时间,领红包的人,时间等。

    难点在于:将红包如何分成几份,等可能的被其他人抢到,而且不会出现为0的红包。

    首先运用取随机数的类,每次取当前所剩金额的随机数,然后将这些分好的红包放到数组中的,随机打乱顺序,这样取得时候就是等可能性,每个人取得时候顺序获得数组中的红包。

    要点1:判断红包金额是否满足最低标准每人一分。2、将红包金额*100,变为整数进行随机数分包处理,最后再/100.0变为2位小数(原因计算机中表示小数double会不精确)。

    代码实现如下:

      1 package zuoye;
      2 import java.text.SimpleDateFormat;
      3 import java.util.*;
      4 public class zuoye_3 {
      5 
      6     public static void main(String[] args) {
      7         Scanner sc=new Scanner(System.in);
      8         Date t=new Date();
      9         Calendar c=Calendar.getInstance();
     10         c.setTime(t);
     11         c.add(Calendar.HOUR, 2);
     12         System.out.println("请输入红包金额:");
     13         double n=sc.nextDouble();
     14         System.out.println("请输入红包的数量:");
     15         int x=sc.nextInt();
     16         //微信红包类
     17         Money hong=new Money(n,c.getTime(),x);
     18         System.out.println("请输入抢红包的人的名字:");
     19         for(int i=0;i<x;i++)
     20         {  people ob1=new people(sc.next());
     21            Date t1=new Date();    
     22           hong.qiang(ob1,t1);
     23         }
     24         hong.show();  //红包结果
     25     }
     26 
     27 }
     28 class Money{
     29     private double money;           //红包金额
     30     private Date time;              //到期时间
     31     private int size;               //红包数
     32     private int num;                //第几个人在抢
     33     private double nowmoney;        //红包剩余金额
     34     ArrayList<Double> str=new ArrayList<Double>(); //红包被分为存取随机金额
     35     ArrayList<people> p=new ArrayList<people>();   //抢红包的人
     36     public Money(double money, Date time, int size) {
     37         super();
     38         this.money = money; 
     39         this.time = time;
     40         this.size = size;
     41         nowmoney=money;
     42         yao();
     43     }
     44     /*
     45      * 将红包分为随机金额存到数组中,并处理如果金额为0时的情况,最后打乱顺序
     46      */
     47     private void yao()
     48     {  if(this.money<0.01*this.size){
     49         System.out.println("error,最低每人1分");
     50         return ;
     51       }
     52         int m=(int)((nowmoney-0.01*this.size)*100);         
     53         for(int i=0;i<size;i++)
     54         {
     55             if(i==size-1)
     56              {str.add(m/100.0);
     57                m=0;
     58              }
     59             else
     60             {
     61                 Random r=new Random();
     62                 int x=0;
     63                 if(m!=0)
     64                 x=r.nextInt(m);
     65                 m-=x;
     66                 str.add(x/100.0);
     67             }
     68             str.set(i,str.get(i)+0.01);
     69         }
     70         Collections.shuffle(str);         //打乱顺序
     71     }
     72     /*
     73      * 抢红包操作,参数为人和时间
     74      */
     75     void qiang(people ob,Date t){
     76         num++;
     77         if(num>size)
     78         {
     79             System.out.println("红包已经抢完了");
     80             return ;
     81         }
     82         if(t.after(time))
     83         {
     84             System.out.println("红包已经过期了");
     85             return ;
     86         }
     87             ob.money=str.get(num-1);
     88             ob.time=t;
     89             nowmoney-=ob.money;
     90             p.add(ob);
     91             if(num==size)
     92                 nowmoney=0;
     93     }
     94     /*
     95      * 显示当前红包情况,被谁抢,什么时候,剩余多少
     96      */
     97     void show()
     98     {
     99         Collections.sort(p);
    100         System.out.print("红包共"+this.money+"元,已有"+p.size()+"人抢,现在剩余");
    101         System.out.printf("%.2f
    ",this.nowmoney);
    102         SimpleDateFormat c=new SimpleDateFormat("h:mm:ss");
    103         for(int i=0;i<p.size();i++)
    104         {
    105             System.out.print(p.get(i).name+"抢到");
    106             System.out.printf("%.2f",p.get(i).money);
    107             System.out.println("  时间"+c.format(p.get(i).time));
    108         }
    109     }
    110 }
    111 class people implements Comparable<people>{
    112     String name;
    113     double money;
    114     Date time;
    115     public people(String name) {
    116         super();
    117         this.name = name;
    118     }
    119     @Override
    120     public int compareTo(people o) {
    121         
    122         return (int)(o.money-this.money);
    123     }
    124     
    125 }
  • 相关阅读:
    springboot 搭建druid数据监控
    spring-boot编写简易mvc
    解决rabbitmq 开启启动报错
    intelij idea 使用maven打包报错 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1
    php foreach循环引用的问题
    手把手编写hyperf JsonRpc demo
    centos8配置nfs教程本机系统mac
    Java基础的练习题
    Java——循环
    Java——数组
  • 原文地址:https://www.cnblogs.com/llsq/p/7436508.html
Copyright © 2011-2022 走看看