期中抽奖

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Random;

public class choujiang {
    ArrayList<Integer> cards;
 public static void main(String[] args) {
  // TODO Auto-generated method stub
        choujiang c = new choujiang();
        c.deal();
        System.out.println("一等奖:");
        c.sj(2);
        System.out.println("
");
        System.out.println("二等奖:");
        c.sj(4);
        System.out.println("
");
        System.out.println("三等奖:");
        c.sj(6);
        System.out.println("");
         }
    public void deal(){
    	if(cards == null){
    	cards = new ArrayList<Integer>();
        for(int i=1;i<=2000;i++){
        cards.add(i);
      }
     }
     Collections.shuffle(cards);
    }
    public void sj(int n){
     Random ran1 = new Random(2000);
                for (int i = 0; i < n; i++) {
         int index = ran1.nextInt(cards.size());
            System.out.print(cards.get(index)+"	");
            cards.remove(index);
        }
    }
}

原文地址:https://www.cnblogs.com/87v5/p/5484477.html