集合框架练习14

package hongbao;

import java.util.Random;
import java.util.TreeSet;

public class TestRed {


    public static void main(String[] args) {
    
makeredbag(100, 3);
        
        

    }

public static void makeredbag(int money,int mount){
    if(money<mount){
        throw new RuntimeException("请重新输入人数");
    }

    TreeSet<Integer> set=new TreeSet<Integer>();
    Random r=new Random();
 while(set.size()<mount-1){
     set.add(r.nextInt(money-1)+1);  
 }
    System.out.println(set);

    int value=0;
    int index=1;
    for(Integer i:set){
        System.out.println(index+++":"+(i-value));
         value=i;
    }
    System.out.println(index+":"+(money-value));

        
}
    
}

 

posted @ 2017-05-10 14:11  苏轼的红烧肉  阅读(97)  评论(0编辑  收藏  举报