天天看点

微信红包程序 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)+"元");

       }

   }

}

继续阅读