• Java第六次作业


    关于抽奖系统的程序如下:

     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 }

    程序运行结果如下:

  • 相关阅读:
    使用init_connect记录MySQL登录日志
    MySQL 在线开启GTID的每个阶段是要做什么
    chrome控制台发送post请求
    口语练习
    adb_usb.ini在adb找不到设备时
    ubuntu 12.04硬盘分区,格式化,挂载
    编译错误处理noproguard.classeswithlocal.dex已杀死
    ubuntu 12.04(gcc降级)编译android代码遇到"_FORTIFY_SOURCE"的解决方法
    ubuntu在update的时候报错GPL ERROR
    工作站环境搭建
  • 原文地址:https://www.cnblogs.com/wawawaawa/p/5486046.html
Copyright © 2020-2023  润新知