java第6次作业
题目:做一个抽奖程序,要求可以设定参与抽奖的总人数和奖项的个数,获奖不可重复。
代码如下:
思路备注在代码中,获奖不可重复,这句代码:people.remove(peopleindex); 并在list不为空的情况下递归。
代码中,提示输出写错了,想表达的是奖品的种类,不是个数。。。。
package com.cumin; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; public class LuckDraw { List<Map<String, String>> list = new ArrayList<>(); Map<String, String> map = new HashMap<>(); public List<Map<String, String>> LuckDrawing(List<Prize> prizes, List<People> people) { Random rm = new Random(); // people List随机的人 int peopleindex = rm.nextInt(People.numofpeople); // prizes List随机的奖品 int prizesindex = rm.nextInt(Prize.numofprizes); // 奖品与抽奖人建立Map map.put(people.get(peopleindex).getName(), prizes.get(prizesindex).getName()); list.add(map); // 不可重复抽奖,从people删除该人 people.remove(peopleindex); //总人数-- People.numofpeople--; if (!people.isEmpty()) { return LuckDrawing(prizes, people); } else return list; } }
package com.cumin; public class People { public static int numofpeople; private String name; static int count = 0; public People(String name) { count++; //判断人数 if (count > numofpeople) { System.out.println("参与抽奖的人数已满,请下次再来!"); } this.name = name; } public static int getNumofpeople() { return numofpeople; } public void setNumofpeople(int numofpeople) { this.numofpeople = numofpeople; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.cumin; public class Prize { // 奖品的数量 public static int numofprizes; private String name; static int count = 0; public Prize(String name) { count++; //判断奖品数量 if (count > numofprizes) { System.out.println("奖品数量已达到最大,无需设置"); } this.name=name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public static int getNumofprizes() { return numofprizes; } public void setNumofprizes(int numofprizes) { this.numofprizes = numofprizes; } }
测试类如下:
package com.cumin; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; public class Test { public static void main(String args[]) { System.out.println("请设置抽奖的人数:"); Scanner input = new Scanner(System.in); People.numofpeople = input.nextInt(); System.out.println("请设置奖品的数量:"); Prize.numofprizes = input.nextInt(); List<People> people = new ArrayList<>(); People people1 = new People("小明"); People people2 = new People("小红"); People people3 = new People("小美"); people.add(people1); people.add(people2); people.add(people3); List<Prize> prizes = new ArrayList<>(); Prize prize1 = new Prize("书"); Prize prize2 = new Prize("橡皮"); prizes.add(prize1); prizes.add(prize2); LuckDraw luckdraw = new LuckDraw(); List<Map<String, String>> list = luckdraw.LuckDrawing(prizes, people); for(int i=0;i<list.size();i++) { Map<String, String> map =list.get(i); Set<String> set = map.keySet(); Iterator<String> its = set.iterator(); while (its.hasNext()) { // key String key = its.next(); // value String value = map.get(key); System.out.println(key +" "+ value); list.remove(i); } } } }
附图如下:
如果在多出人数和奖品,会提示,附图如下: