当前位置:首页 > 代码 > 正文

抽奖案例代码(随机抽奖程序代码)

admin 发布:2022-12-19 21:53 182


今天给各位分享抽奖案例代码的知识,其中也会对随机抽奖程序代码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

抽奖程序(用vb编写)

在窗体上添加一个command1,一个timer1,label1(0~5)

控件数组(添加一个label1,然后再复制5个,共6个,用来显示数字),代码如下:

Private

Sub

Command1_Click()

If

Command1.Caption

=

"抽奖"

Then

Command1.Caption

=

"停止"

Timer1.Enabled

=

True

Else

Timer1.Enabled

=

False

Command1.Caption

=

"抽奖"

End

If

End

Sub

Private

Sub

Form_Load()

Command1.Caption

=

"抽奖"

Timer1.Interval

=

50

Timer1.Enabled

=

False

End

Sub

Private

Sub

Timer1_Timer()

Randomize

For

i

=

To

5

Label1(i)

=

Int(10

*

Rnd)

Select

Case

Label1(i).Caption

Case

Label1(i).BackColor

=

RGB(0,

0,

0)

Label1(i).ForeColor

=

RGB(255,

255,

255)

Case

1

Label1(i).BackColor

=

RGB(128,

42,

42)

Label1(i).ForeColor

=

RGB(127,

213,

213)

Case

2

Label1(i).BackColor

=

RGB(255,

0,

0)

Label1(i).ForeColor

=

RGB(0,

255,

255)

Case

3

Label1(i).BackColor

=

RGB(255,

97,

0)

Label1(i).ForeColor

=

RGB(0,

158,

255)

Case

4

Label1(i).BackColor

=

RGB(255,

255,

0)

Label1(i).ForeColor

=

RGB(0,

0,

255)

Case

5

Label1(i).BackColor

=

RGB(0,

255,

0)

Label1(i).ForeColor

=

RGB(255,

0,

255)

Case

6

Label1(i).BackColor

=

RGB(0,

0,

255)

Label1(i).ForeColor

=

RGB(255,

0,

0)

Case

7

Label1(i).BackColor

=

RGB(160,

32,

240)

Label1(i).ForeColor

=

RGB(95,

223,

15)

Case

8

Label1(i).BackColor

=

RGB(192,

192,

192)

Label1(i).ForeColor

=

RGB(63,

63,

63)

Case

9

Label1(i).BackColor

=

RGB(255,

255,

255)

Label1(i).ForeColor

=

RGB(0,

0,

0)

End

Select

Next

End

Sub

Java代码实现抽奖:从班级的学号中抽出一个一等奖,两个二等奖,三个三等奖

抽取问题, 重点是 同一个学号不能重复被抽取.

解决办法很多,

比如数组可以使用下标来标记,号码是否被使用,使用了就继续下一次抽取

也可以使用集合来抽取,把集合顺序打乱,然后随便抽几个就可以了

参考代码:数组法

import java.util.Random;

public class Test {

public static void main(String[] args) {

int stuNums=30;

int[] nums=new int[stuNums];//存储学号的数组

boolean[] flags=new boolean[stuNums];//标记,用于标记对应下标的学号是否已经被抽取过了

for (int i = 0; i  stuNums; i++) {

nums[i]=i+1;//给学号赋值

}

Random r=new Random();

while(true){

int index = r.nextInt(stuNums);

if(!flags[index]){

System.out.println("A等:"+nums[index]);

flags[index]=true; //标记已经被使用过了

break;

}

}

for (int i = 0; i  2; i++) {

int index = r.nextInt(stuNums);

if(!flags[index]){

System.out.println("B等:"+nums[index]);

flags[index]=true;

}else{

i--;//如果已经被抽取过了 ,那么i建议,再次循环

}

}

for (int i = 0; i  3; i++) {

int index = r.nextInt(stuNums);

if(!flags[index]){

System.out.println("c等:"+nums[index]);

flags[index]=true;

}else{

i--;

}

}

}

}

集合法

import java.util.ArrayList;

import java.util.Collections;

public class Test2 {

public static void main(String[] args) {

int stuNums=20;

ArrayListInteger list=new ArrayListInteger();

for (int i = 0; i  stuNums; i++) {

list.add(i+1);

}

System.out.println("有序"+list);

Collections.shuffle(list);//打乱顺序

System.out.println("乱序"+list);

System.out.println("A等"+list.get(0));

System.out.println("B等"+list.get(1));

System.out.println("B等"+list.get(2));

System.out.println("C等"+list.get(3));

System.out.println("C等"+list.get(4));

System.out.println("C等"+list.get(5));

}

}

用c语言编写一个简易的抽奖程序,

用data.txt文件保存以下内容:

13725528132 李桂荣

13725528131 李二来

13725528133 张荣刚

13725528130 荣南

13725528137 王三

13725528138 吴立

13725528139 郭德纲

13725528140 周星驰

13725528141 张曼玉

13725528142 张艺谋

13725528152 秦香莲

13725528162 潘金莲

13725528172 李大嘴

13725528182 展堂

//源代码如下

#include stdio.h

#include stdlib.h

#include time.h

#define MAX_NUM 9999

//定义保存人名和电话的数据结构

struct Person

{

char name[20];

char telno[15];

char award;

};

int num = 0; //统计人数

FILE *fp; //文件指针

Person persons[MAX_NUM]; //定义数组

int awarder_1[1] = {-1}; //一等奖

int awarder_2[2] = {-1, -1}; //二等奖

int awarder_3[5] = {-1, -1, -1, -1, -1};//三等奖

//读取文件

void readdata()

{

int i = 0; //数组下标

Person person;

//文件打开

fp = fopen("data.txt", "r");

if (fp == NULL)

{

printf("打开文件data.txt失败!\n");

return;

}

//当文件不为空

while (!feof(fp))

{

num ++;

fscanf(fp, "%s", person.telno);

fscanf(fp, "%s", person.name);

person.award = 'F';

persons[i++] = person;

}

}

//初始化标识

void init()

{

for(int i = 0; i num; i++)

{

persons[i].award = 'F';

}

}

//显示单个中奖信息

void info( int i)

{

printf("手机号码: %s 姓名: %s\n", persons[i].telno, persons[i].name);

}

void main()

{

char again = 'Y';

//读取文件

readdata();

printf("简单抽奖程序\n");

srand((long)time(0));

while(again == 'Y' || again == 'y')

{

//初始化标识

init();

printf("\n开始抽第一等奖(1名),按任意键开始...\n");

getchar();

awarder_1[0] = abs(rand() % num);

while (persons[awarder_1[0]].award == 'T')

{

awarder_1[0] = rand() % num;

}

persons[awarder_1[0]].award = 'T';

info(awarder_1[0]);

printf("\n开始抽第二等奖(2名)\n");

for (int i = 0; i 2; i++)

{

printf("\n第%d个二等奖,按任意键开始...\n", i+1);

getchar();

awarder_2[i] = rand() % num;

while (persons[awarder_2[i]].award == 'T')

{

awarder_2[i] = rand() % num;

}

persons[awarder_2[i]].award = 'T';

info(awarder_2[i]);

}

printf("\n\n开始抽第三等奖(5名)\n");

for (i = 0; i 5; i++)

{

printf("\n第%d个三等奖,按任意键开始...\n", i + 1);

getchar();

awarder_3[i] = rand() % num;

while (persons[awarder_3[i]].award == 'T')

{

awarder_3[i] = rand() % num;

}

persons[awarder_3[i]].award = 'T';

info(awarder_3[i]);

}

printf("\n是否重新开始抽奖?(Y or N)...\n");

again = getchar();

}

getchar();

return;

}

抽奖案例代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于随机抽奖程序代码、抽奖案例代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/26191.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载