这个抽奖系统是一次性抽取完,只要输入抽取人数,系统自动抽取指定数目的名单。
//中奖用户 currentLotteryNum = 0;
// tempUserName[currentLotteryNum-1]=userName[randomNum];//将值赋给存放中奖用户的表中去
while (currentLotteryNum < setLotteryNum)
{
isRepeat = false;
Random kd = new Random();
randomNum = kd.Next(totalCountStaff);//s随机获得个值
if (tempUserName.Length > 0)
{
for (int i = 0; i < currentLotteryNum; i++)
{
if (tempUserName[i] == userName[randomNum])//比较人员是否重复
{
isRepeat = true;
break;
}
}
}
if (!isRepeat)//没有重复添加到临时列表中去
{
// textBox1.AppendText(userName[randomNum]+"\r\n");
currentLotteryNum = currentLotteryNum + 1;
tempUserName[currentLotteryNum - 1] = userName[randomNum];
// tempUserName[currentLotteryNum-1]=userName[randomNum];//将值赋给存放中奖用户的表中去
while (currentLotteryNum < setLotteryNum)
{
isRepeat = false;
Random kd = new Random();
randomNum = kd.Next(totalCountStaff);//s随机获得个值
if (tempUserName.Length > 0)
{
for (int i = 0; i < currentLotteryNum; i++)
{
if (tempUserName[i] == userName[randomNum])//比较人员是否重复
{
isRepeat = true;
break;
}
}
}
if (!isRepeat)//没有重复添加到临时列表中去
{
// textBox1.AppendText(userName[randomNum]+"\r\n");
currentLotteryNum = currentLotteryNum + 1;
tempUserName[currentLotteryNum - 1] = userName[randomNum];
}
编写思路: 通过循环来判断已经抽到奖的人数与设置中将人数经行比较,在抽奖过程中,每个新的抽奖用户都将与已抽取的用户经行比较,避免出现重复抽取,不重复就添加到已抽取的用户列表中去。循环一直进行到抽取列表人数和设定抽奖人数相同位置。
下载: