用Java解决不同等级的抽奖问题(抽中的员工便不能再抽)

问题:(某单位现有100名员工,他们的工号从0001-0100。假若在年末晚会上要组织抽奖活动:根据工号随机抽出20名幸运奖、10名三等奖、7名二等奖、2名一等奖、1名特等奖,抽奖规则是:先抽级别低的奖项)

import java.util.ArrayList;

public class lab4_6 {
	public static void main(String[] args) {
		ArrayList<String> all = new ArrayList<String>();//所有员工的数组列表
		ArrayList<String> third = new ArrayList<String>();//10名“三等奖”员工的数组列表
		ArrayList<String> second = new ArrayList<String>();//7名“二等奖”员工的数组列表
		ArrayList<String> first = new ArrayList<String>();//2名“一等奖”员工的数组列表
		ArrayList<String> special = new ArrayList<String>();//1名“特等奖”员工的数组列表
		String string;//用于创建all内的数组列表内容

		for(int i = 1;i<=100;i++) {
			if(i<10) {
				string = "000"+(int)(i);
			}else if(i<100){
				string = "00"+(int)(i);
			}else string = "0" + (int)(i);
			all.add(string);
		}//往数组列表里装入100个人
		
		//关键思路:如果随机数重复了,使用索引找下标会返回-1,后移除方法会出错,但如果加入if结构判断索引是否为-1
		//巧妙地利用循环机制与continue,就能成功地解除题目
		
		String emploryees;//用于装入抽中的员工所对应的字符串
		for(int i = 1;i <= 10;i++) {//10名三等奖员工
			int test = (int)Math.ceil(Math.random()*(100-1+1)+1);//产生1-100的随机数包含上下限
			if(test<10) {//规范员工编号的格式
				emploryees = "000"+test;
				if(all.indexOf(emploryees)==-1) {
					i--;
					continue;
				}else {
					all.remove(all.indexOf(emploryees));//利用索引移除已中奖人员
				}
			}else if(test<100){
				emploryees = "00"+test;
				if(all.indexOf(emploryees)==-1) {
					i--;
					continue;
				}else {
					all.remove(all.indexOf(emploryees));
				}
			}else {
				emploryees = "0" + test;
				if(all.indexOf(emploryees)==-1) {
					i--;
					continue;
				}else {
					all.remove(all.indexOf(emploryees));
				}
			}
			third.add(emploryees);	
		}
		
		for(int i = 1;i <= 7;i++) {//7名二等奖员工
			int test = (int)Math.ceil(Math.random()*(100-1+1)+1);
			if(test<10) {
				emploryees = "000"+test;
				if(all.indexOf(emploryees)==-1) {
					i--;
					continue;
				}else {
					all.remove(all.indexOf(emploryees));
				}
			}else if(test<100){
				emploryees = "00"+test;
				if(all.indexOf(emploryees)==-1) {
					i--;
					continue;
				}else {
					all.remove(all.indexOf(emploryees));
				}
			}else {
				emploryees = "0" + test;
				if(all.indexOf(emploryees)==-1) {
					i--;
					continue;
				}else {
					all.remove(all.indexOf(emploryees));
				}
			}
			second.add(emploryees);	
		}
		
		
		for(int i = 1;i <= 2;i++) {//2名一等奖员工
			int test = (int)Math.ceil(Math.random()*(100-1+1)+1);
			if(test<10) {
				emploryees = "000"+test;
				if(all.indexOf(emploryees)==-1) {
					i--;
					continue;
				}else {
					all.remove(all.indexOf(emploryees));
				}
			}else if(test<100){
				emploryees = "00"+test;
				if(all.indexOf(emploryees)==-1) {
					i--;
					continue;
				}else {
					all.remove(all.indexOf(emploryees));
				}
			}else {
				emploryees = "0" + test;
				if(all.indexOf(emploryees)==-1) {
					i--;
					continue;
				}else {
					all.remove(all.indexOf(emploryees));
				}
			}
			first.add(emploryees);	
		}
		
		
		for(int i = 1;i <= 1;i++) {//1名特等奖员工
			int test = (int)Math.ceil(Math.random()*(100-1+1)+1);
			if(test<10) {
				emploryees = "000"+test;
				if(all.indexOf(emploryees)==-1) {
					i--;
					continue;
				}else {
					all.remove(all.indexOf(emploryees));
				}
			}else if(test<100){
				emploryees = "00"+test;
				if(all.indexOf(emploryees)==-1) {
					i--;
					continue;
				}else {
					all.remove(all.indexOf(emploryees));
				}
			}else {
				emploryees = "0" + test;
				if(all.indexOf(emploryees)==-1) {
					i--;
					continue;
				}else {
					all.remove(all.indexOf(emploryees));
				}
			}
			special.add(emploryees);	
		}
		System.out.println("10名“三等奖”员工:" + third);
		System.out.println("7名“二等奖”员工:" + second);
		System.out.println("2名“一等奖”员工:" + first);
		System.out.println("1名“特等奖”员工:" + special);
		System.out.println("未中奖员工:" + all);	
	}

}

其中一种结果:

10名“三等奖”员工:[0013, 0087, 0072, 0045, 0061, 0075, 0009, 0036, 0095, 0037]
7名“二等奖”员工:[0062, 0029, 0024, 0059, 0049, 0030, 0035]
2名“一等奖”员工:[0004, 0093]
1名“特等奖”员工:[0076]
未中奖员工:[0001, 0002, 0003, 0005, 0006, 0007, 0008, 0010, 0011, 0012, 0014, 0015, 0016, 0017, 0018, 0019, 0020, 0021, 0022, 0023, 0025, 0026, 0027, 0028, 0031, 0032, 0033, 0034, 0038, 0039, 0040, 0041, 0042, 0043, 0044, 0046, 0047, 0048, 0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057, 0058, 0060, 0063, 0064, 0065, 0066, 0067, 0068, 0069, 0070, 0071, 0073, 0074, 0077, 0078, 0079, 0080, 0081, 0082, 0083, 0084, 0085, 0086, 0088, 0089, 0090, 0091, 0092, 0094, 0096, 0097, 0098, 0099, 0100]

部分代码参考java吧的@无奈的月月鸟,参考网址https://tieba.baidu.com/p/7056864615

Java萌新,代码若是有问题还请大佬指教。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值