public class lottery { public static void main(String[] args) { List<String> award = new ArrayList<>(); award.add("一等奖"); award.add("二等奖"); award.add("二等奖"); award.add("二等奖"); award.add("三等奖"); award.add("三等奖"); award.add("三等奖"); award.add("三等奖"); System.out.println(award); //定义一个map集合,用于存放中间人id和奖项 Map<Integer,String> m = new HashMap<>(); //录入多个抽奖人姓名,中间用|隔开 Scanner sc = new Scanner(System.in); System.out.println("请录入抽奖人姓名(用|分隔开):"); String name = sc.nextLine(); String[] names = name.split("\\|"); System.out.println("抽奖人姓名为:"); for (String s : names) { System.out.print(s+","); } System.out.println("输入yes或者no来确认抽奖人姓名"); String ensure = sc.next(); if (ensure.equals("yes")){ Random r = new Random(); Map<Integer,String> map = new HashMap<>(); for (int i = 0; i < names.length; i++) { int id = r.nextInt(9); if (map.containsKey(id)){ i--; continue; }else { map.put(id,names[i]); } } System.out.println(map); Set<Integer> integers = map.keySet(); Object[] objects = integers.toArray(); //进行抽奖 int count = 0;//用来标记已经抽奖人数 int number = 6;//奖池中奖项个数 do{ int num = r.nextInt(number); String s2 = award.get(num); //抽中一个,应该删除award集合中对应奖项 System.out.println("s2="+s2); String remove = award.remove(num); System.out.println(remove+","+award); number--; m.put((Integer) objects[count],s2); count++; System.out.println("输入next,进行下一次抽奖"); }while ((number>0)&&(count<map.size())&&sc.next().equals("next")); System.out.println(m); }else if (ensure.equals("no")){ System.out.println("抽奖人未确认,请重新启动程序!"); } } }
1、抽奖软件
最新推荐文章于 2024-08-19 16:01:20 发布