
请实现以下抽奖的需求:
基本要求为:
请实现以下抽奖的需求:
package com.company;
import redis.clients.jedis.*;
import java.util.ArrayList;
public class Main {
final static String host = new String("localhost");
final static int port = 6379;
final static String Lottery = "0233Redis:Sets:id:lottery";
final static String Winner = "0233Redis:HashMap:Winner:id";
final static String Winner_First = "0233Redis:HashMap:id01:01";
final static String Winner_Second = "0233Redis:HashMap:id02:02";
final static String Winner_Third = "0233Redis:HashMap:id03:03";
static Jedis jedis = new Jedis(host,port);
public static void main(String[] args) {
//CleanAll();
SetName(30);
GetName();
//ShowAll();
}
//设定候选人
static void SetName(int x){
for(int i=1;i<=x;i++){
jedis.sadd(Lottery,"候选人"+Integer.toString(i));
}
System.out.println("候选人登记完毕!");
}
//抽奖过程
static void GetName(){
//temp_1:记录一二三等奖的中文形式
//temp_2:接收一个从set中取出的随机名字
//temp_4:记录获奖组的key值
//y:规定每一组获奖者的人数
System.out.println("所有参加抽奖的候选人"+jedis.smembers(Lottery));
String temp_1,temp_2,temp_4 = null;
int y;
//创建一个数组来存储每组获奖者
ArrayList arr = new ArrayList(5);
for(int j=1;j<=3;j++)
{
if(j==1){temp_1="一等奖";y=1;temp_4=Winner_First;}
else if(j==2){temp_1="二等奖";y=3;temp_4=Winner_Second;}
else {temp_1="三等奖";y=5;temp_4=Winner_Third;}
System.out.print(temp_1+"获得者是:");
for(int i=0;i
temp_2 = jedis.spop(Lottery);
if(temp_2==null) {
System.out.println(temp_1+"数据为空!");
return ;
}
arr.add(temp_2);
System.out.print(temp_2+" ");
}
//用hashmap存储每一组获奖者
jedis.hset(Winner,temp_4, String.valueOf(arr));
//每存储完一组获奖者,数组都需要清空,来记录下一组获奖者
arr.clear();
System.out.print("n");
}
}
//清空所有数据结构,测试用
static void CleanAll(){
jedis.del(Lottery);
jedis.del(Winner);
System.out.println("清理完成");
}
//展示存储情况,测试用
static void ShowAll(){
//temp_3:表示从链表中接收的弹出的数据
//temp_5:接收一二三等奖链表的名称
String temp_3,temp_5;
for(int j=1;j<=3;j++)
{
if(j==1){
temp_5=Winner_First;
System.out.println("一等奖获取者是:");}
else if(j==2){
temp_5=Winner_Second;
System.out.println("二等奖获取者是:");}
else {
temp_5=Winner_Third;
System.out.println("三等奖获取者是:");}
System.out.println(jedis.hget(Winner,temp_5));
}
}
}
代码还不够美观,方法都是静态的,被老师批过,其他没啥问题
手动高亮:仅供参考学习,请勿照搬
欢迎讨论