标签:
关于抽奖系统的程序如下:
1 import java.util.HashSet; 2 import java.util.Iterator; 3 import java.util.Random; 4 import java.util.Scanner; 5 6 public class choujiang { 7 private HashSet<Integer>set; 8 private Random rand; 9 private Scanner sc; 10 11 public choujiang(){ 12 set = new HashSet<>(); 13 rand = new Random(); 14 sc = new Scanner(System.in); 15 } 16 17 public void addToSte(){ 18 System.out.println("~~欢迎大家参与本次抽奖活动~~"); 19 System.out.println("~~本活动上限人数:1000人~~"); 20 System.out.println("请输入总奖品数目:"); 21 int total = sc.nextInt(); 22 while(set.size()<total){ 23 set.add(rand.nextInt(1000)); 24 } 25 } 26 27 public void drawPrize(){ 28 System.out.println("分别按顺序输入一、二、三等奖的获奖人数:"); 29 int prize1 = sc.nextInt(); 30 int prize2 = sc.nextInt(); 31 int prize3 = sc.nextInt(); 32 Iterator iterator = set.iterator(); 33 34 int i =0; 35 System.out.println("~~恭喜以下参与者获得一等奖~~"); 36 while(i<prize1){ 37 System.out.println("参与者号码:"+iterator.next()); 38 iterator.remove(); 39 i++; 40 } 41 42 i=0; 43 System.out.println("~~恭喜以下参与者获得二等奖~~"); 44 while(i<prize2){ 45 System.out.println("参与者号码:"+iterator.next()); 46 iterator.remove(); 47 i++; 48 } 49 50 i=0; 51 System.out.println("~~恭喜以下参与者获得三等奖~~"); 52 while(i<prize3){ 53 System.out.println("参与者号码:"+iterator.next()); 54 iterator.remove(); 55 i++; 56 } 57 System.out.println("~~本次活动已结束,谢谢各位的参与~~"); 58 } 59 60 public static void main(String[] args) { 61 choujiang setLearning = new choujiang(); 62 setLearning.addToSte(); 63 setLearning.drawPrize(); 64 } 65 66 }
程序运行结果如下:
标签:
原文地址:http://www.cnblogs.com/wawawaawa/p/5486046.html