微信红包程序 java

package test;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;


public class test {

   public static void main(String[] args) {
       int  money  = 1500;//红包金额
       int people = 11;//参与人数
       if(people >money){
           System.out.println("红包金额必须大于等于参与人数");
       }else{
           System.out.println("红包金额:"+(money/100)+"元,参与人数:"+people);
           int index = 1;
           Map<String, String> map = new HashMap<String, String>();
           while(true){
                int   f1  =0;
                if(people == money){
                    f1 = 1;
                }else{
                     while(true){
                         f1   =(int)(Math.random()* money);
//                         System.out.println(f1+"/"+people+"/"+money);
                         if(f1 > 0 && people <= (money-f1)){
                             break;
                         }
                     }
                }
//                System.out.println("随机数:"+f1+"/当时的钱:"+money+"/当时的人数:"+people);
                if(people > 2){
                    money = money- f1;
                    people = people-1;
//                    System.out.println(money+"/"+people);
                    map.put((index++)+"", f1+"");
               }else if(people == 2){
                    money = money- f1;
                    people = people-1;
//                    System.out.println(money+"/"+people);
                    map.put((index++)+"", f1+"");
                    map.put((index++)+"", money+"");
                    break;
               }else if(people == 1){
                   map.put((index++)+"", money+"");
                   break;
              }
           }
           int i=0 ;
            index =1 ;
           for (Entry<String,String> string : map.entrySet()) {
               i=i+Integer.parseInt(string.getValue());
                System.out.println("当前第"+(index++)+"人,抢到的红包金额为:"+(Double.valueOf(string.getValue())/100)+"元");
           }
            System.out.println("总金额:"+(i/100)+"元");
       }
   }
   
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值