欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

幸运抽奖小游戏,

来源: javaer 分享于  点击 39053 次 点评:45

幸运抽奖小游戏,


随机回去10位幸运者的名单,依次递减,最后一位就是幸运者

 

 

 

 

  1 package com.lottery.controller;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.FileReader;
  5 import java.io.IOException;
  6 import java.util.ArrayList;
  7 import java.util.HashSet;
  8 import java.util.List;
  9 import java.util.Set;
 10 
 11 /**
 12  * 幸运抽奖
 13  */
 14 public class Lottery {
 15     private static final String NAME_LIST = "./resources/nameList.txt";
 16     private static Lottery lottery = new Lottery();
 17     static boolean flag = true;    // 标记
 18 
 19     /**
 20      * 每次筛选掉一人
 21      */
 22     public static void getDel() {
 23         Set<String> set = lottery.getRandom();
 24         List<String> list = new ArrayList<>();
 25         list.addAll(set);
 26 
 27         System.out.println("********抽奖开始********\n");
 28 
 29         for (int i = 0; i < 10; i++) {
 30 
 31             try {
 32                 Thread.sleep(2000);
 33             } catch (InterruptedException e) {
 34             }
 35 
 36             System.out.println("   有 " + (10 - i) + " 位幸运者成功入选!\n");
 37             int num = (int) (Math.random() * list.size());
 38 
 39             for (String name : list) {
 40                 System.out.println("\t" + name + "\n");
 41             }
 42             list.remove(num);
 43         }
 44 
 45     }
 46 
 47     /**
 48      * 获取10 位幸运抽奖者
 49      */
 50     public Set<String> getRandom() {
 51         // 获取名单
 52         List<String> list = getTextFile(NAME_LIST);
 53         Set<String> lucky = new HashSet<>();
 54 
 55         System.err.println("\t抽奖总人数:" + list.size() + "位\n");
 56 
 57         while (flag) {
 58             // 在集合中 生成随机的抽奖名单
 59             int numb = (int) (Math.random() * list.size());
 60             lucky.add(list.get(numb));
 61 
 62             // 获取10位幸运者
 63             if (lucky.size() >= 10) {
 64                 flag = false;
 65             }
 66         }
 67         return lucky;
 68 
 69     }
 70 
 71     /**
 72      * 获取抽奖名单
 73      * 
 74      * @param fileName
 75      * @return
 76      */
 77     public List<String> getTextFile(String fileName) {
 78         List<String> list = new ArrayList<>();
 79         BufferedReader br = null;
 80         try {
 81             FileReader fr = new FileReader(fileName);
 82             br = new BufferedReader(fr);
 83 
 84             String str = null;
 85             while ((str = br.readLine()) != null) {
 86                 // 判断若不为空就添加到集合中
 87                 if (!str.trim().equals("")) {
 88                     // 读取抽奖名单,并添加到集合
 89                     list.add(str);
 90                 }
 91             }
 92         } catch (IOException e) {
 93             e.printStackTrace();
 94         } finally {
 95             if (br != null) {
 96                 try {
 97                     br.close();
 98                 } catch (IOException e) {
 99                     e.printStackTrace();
100                 }
101             }
102         }
103         return list;
104     }
105 
106 }

 

 1 package com.lottery.view;
 2 
 3 import java.util.Scanner;
 4 
 5 import com.lottery.controller.Lottery;
 6 
 7 /**
 8  * 菜单
 9  */
10 public class LotteryView {
11     /**
12      * 主菜单
13      */
14     public static void mainMenu() {
15         boolean loopflag = true;
16         do {
17             getWelcome();
18             char key = getUserAction();
19             switch (key) {
20             case '1':
21                 Lottery.getDel();
22                 break;
23             case '2':
24                 System.err.print("是否确认退出程序![Y/N]:");
25                 char action = getUserAction();
26                 if (action == 'Y') {
27                     loopflag = false;
28                 }
29                 System.out.println("成功幸运抽奖程序!");
30                 break;
31             }
32         } while (loopflag);
33     }
34 
35     /**
36      * 欢迎页
37      */
38     public static void getWelcome() {
39         System.out.println("******** 欢迎使用幸运抽奖 ********\n");
40         System.out.println(" \t 1.开始抽奖\n");
41         System.out.println(" \t 2.退出抽奖\n");
42         System.out.println("********************************\n");
43         System.out.println(" \t 要继续吗.....");
44     }
45 
46     /**
47      * 读取键盘键入值(每次只取键入序列的第一个键值)
48      *
49      * @return
50      */
51     @SuppressWarnings("resource")
52     public static char getUserAction() {
53         // 数组存储所有有效的字符
54         char[] validkey = { '1', '2', 'N', 'Y' };
55         Scanner scan = new Scanner(System.in);
56         while (true) {
57             // 接收字符并转换成大写
58             String str = scan.next().toUpperCase();
59             if (str.length() != 1) {
60                 continue;
61             }
62             char key = str.charAt(0);
63 
64             // 判断键盘接收的是否更有效字符是否相同
65             for (char k : validkey) {
66                 if (k == key) {
67                     return key;
68                 }
69             }
70         }
71     }
72 
73 }

 

 1 package com.lottery.test;
 2 
 3 import com.lottery.view.LotteryView;
 4 
 5 /**
 6  * 测试
 7  */
 8 public class LotteryTest {
 9     public static void main(String[] args) {
10         LotteryView.mainMenu();
11     }
12 }

 

相关文章

    暂无相关文章
相关栏目:

用户点评