如何使用javascript做一个简单的抽奖程序

如何使用javascript做一个简单的抽奖程序,第1张

假定开始按钮是 Command1,两个文本框分别是 Text1、Text2

Private Sub Command1_Click()

Randomize

Text1Text = Int(Rnd 20) + 1

Text2Text = Int(Rnd 20) + 1

End Sub

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

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

Private

Sub

Command1_Click()

If

Command1Caption

=

"抽奖"

Then

Command1Caption

=

"停止"

Timer1Enabled

=

True

Else

Timer1Enabled

=

False

Command1Caption

=

"抽奖"

End

If

End

Sub

Private

Sub

Form_Load()

Command1Caption

=

"抽奖"

Timer1Interval

=

50

Timer1Enabled

=

False

End

Sub

Private

Sub

Timer1_Timer()

Randomize

For

i

=

0

To

5

Label1(i)

=

Int(10

Rnd)

Select

Case

Label1(i)Caption

Case

0

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

在窗体中放入label、button、timer、memo 四种类型的控件各一个:

label1--用于显示手机号码的滚动

timer1(时钟)--用于产生随机的手机号码

button1--点击即抽奖,并将获奖号码保存到memo1中

memo1--用于保存获奖手机号码

unit Unit1;

interface

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, ExtCtrls;

type

  TForm1 = class(TForm)

    Label1: TLabel;

    Button1: TButton;

    Timer1: TTimer;

    Memo1: TMemo;

    procedure Timer1Timer(Sender: TObject);

    procedure FormCreate(Sender: TObject);

    procedure Button1Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

const

n=20;

a:array[1n] of string[11]=('13607193000','13607193001',

'13607193002','13607193003','13607193004',

'13607193005','13607193006','13607193007',

'13607193008','13607193009',

'13607193010','13607193011','13607193012',

'13607193013','13607193014',

'13607193015','13607193016','13607193017',

'13607193018','13607193019');  

var

  Form1: TForm1;

implementation

{$R dfm}

procedure TForm1Timer1Timer(Sender: TObject);

var

k:integer;

begin

k:=trunc(nrandom+1);

  label1Caption:=a[k];

end;

procedure TForm1FormCreate(Sender: TObject);

begin

      randomize;

end;

procedure TForm1Button1Click(Sender: TObject);

begin

  memo1LinesAdd(label1Caption);

end;

end

#include<stdioh>

#include <conioh>

#include <stdlibh>

#include <memoryh>

#include <stringh>

#include <timeh>

#define AWARDER_NUM 10

typedef struct tag_award

{

char ID[20];

char name[20];

char telNo[20];

char address[50];

}AWARDER;

AWARDER awarder = new AWARDER[AWARDER_NUM];

int first_award[1] = {-1};

int second_award[2] = {-1,-1};

int third_award[5] ={-1,-1,-1,-1,-1};

int awarder_num = 0;

void info_login()

{

if(awarder_num >= AWARDER_NUM)

{

AWARDER new_awarder = new AWARDER[awarder_num + AWARDER_NUM];

memcpy(new_awarder,awarder,awarder_numsizeof(AWARDER));

delete awarder;

awarder = new_awarder;

}

printf("身份z号:");

scanf("%s",awarder[awarder_num]ID);

printf("姓名:");

scanf("%s",awarder[awarder_num]name);

printf("电话号码:");

scanf("%s",awarder[awarder_num]telNo);

printf("家庭住址:");

scanf("%s",awarder[awarder_num]address);

awarder_num++;

}

void info_delete()

{

char name[20];

printf("输入您要删除的抽奖者的姓名:");

scanf("%s",name);

for(int i=0;i<awarder_num;i++)

{

if (strcmp(awarder[i]name,name) == 0)

{

printf("您选择删除的抽奖者的信息如下:\n");

printf("身份z号:%s\n",awarder[i]ID);

printf("姓名:%s\n",awarder[i]name);

printf("电话号码:%s\n",awarder[i]telNo);

printf("家庭住址:%s\n",awarder[i]address);

break;

}

}

if(i == awarder_num)

{

printf("没有找到您输入的抽奖者!\n");

}

else

{

char c;

printf("您确定要删除吗?(Y//N)");

fflush(stdin);

scanf("%c",&c);

if(c == 'Y' || c == 'y')

{

AWARDER new_award = new AWARDER[awarder_num-1];

if(i <= 0)

memcpy(new_award,awarder+1,sizeof(AWARDER) (awarder_num - 1));

else if( i >= awarder_num -1)

memcpy(new_award,awarder,sizeof(AWARDER) (awarder_num - 1));

else

{

memcpy(new_award,awarder,sizeof(AWARDER) i);

memcpy(new_award+i,awarder+i+1,sizeof(AWARDER) (awarder_num - i - 1));

}

awarder_num--;

delete awarder;

awarder = new_award;

}

}

}

void info_modify()

{

char name[20];

printf("输入您要修改的抽奖者的姓名:");

scanf("%s",name);

for(int i=0;i<awarder_num;i++)

{

if (strcmp(awarder[i]name,name) == 0)

{

printf("您选择的抽奖者的信息如下:\n");

printf("身份z号:%s\n",awarder[i]ID);

printf("姓名:%s\n",awarder[i]name);

printf("电话号码:%s\n",awarder[i]telNo);

printf("家庭住址:%s\n",awarder[i]address);

break;

}

}

if(i == awarder_num)

{

printf("没有找到您输入的抽奖者!\n");

}

else

{

printf("请输入该抽奖者的新信息:\n");

printf("身份z号:");

scanf("%s",awarder[i]ID);

printf("姓名:");

scanf("%s",awarder[i]name);

printf("电话号码:");

scanf("%s",awarder[i]telNo);

printf("家庭住址:");

scanf("%s",awarder[i]address);

}

}

bool check_over(int cur_award,int awarderNo)

{

if(awarderNo == 1)

{

if(cur_award == first_award[0])

return true;

}

else if(awarderNo <= 3)

{

if(cur_award == first_award[0])

return true;

for(int i=0;i<awarderNo-1;i++)

if(cur_award == second_award[i])

return true;

}

else if(awarderNo <= 8)

{

if(cur_award == first_award[0])

return true;

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

if(cur_award == second_award[i])

return true;

for(i=0;i<awarderNo-3;i++)

if(cur_award == third_award[i])

return true;

}

return false;

}

void award()

{

if(awarder_num < 8)

{

printf("人数太少,请添加足够的人再抽奖!\n");

return;

}

srand((long)time(0));

char hit_id[20];

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

getch();

while(!kbhit())

{

first_award[0] = rand() % awarder_num;

memcpy(hit_id,awarder[first_award[0]]ID,18);

printf("\r ");

printf("\r %s",hit_id);

}

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

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

{

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

getch();

while(!kbhit())

{

second_award[i] = rand() % awarder_num;

while(check_over(second_award[i],1+i))

second_award[i] = rand() % awarder_num;

memcpy(hit_id,awarder[second_award[i]]ID,18);

printf("\r ");

printf("\r %s",hit_id);

}

}

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

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

{

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

getch();

while(!kbhit())

{

third_award[i] = rand() % awarder_num;

while(check_over(third_award[i],3+i))

third_award[i] = rand() % awarder_num;

memcpy(hit_id,awarder[third_award[i]]ID,20);

printf("\r ");

printf("\r %s",hit_id);

}

}

}

void info_show()

{

printf("一等奖获得者:\n");

printf("\t身份z号:%s\n",awarder[first_award[0]]ID);

printf("\t姓名:%s\n",awarder[first_award[0]]name);

printf("\t电话号码:%s\n",awarder[first_award[0]]telNo);

printf("\t家庭住址:%s\n",awarder[first_award[0]]address);

printf("二等奖获得者:\n");

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

{

printf("第%d个二等奖获得者:\n");

printf("\t身份z号:%s\n",awarder[second_award[i]]ID);

printf("\t姓名:%s\n",awarder[second_award[i]]name);

printf("\t电话号码:%s\n",awarder[second_award[i]]telNo);

printf("\t家庭住址:%s\n",awarder[second_award[i]]address);

}

printf("三等奖获得者:\n");

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

{

printf("第%d个三等奖获得者:\n");

printf("\t身份z号:%s\n",awarder[third_award[i]]ID);

printf("\t姓名:%s\n",awarder[third_award[i]]name);

printf("\t电话号码:%s\n",awarder[third_award[i]]telNo);

printf("\t家庭住址:%s\n",awarder[third_award[i]]address);

}

}

/提示信息/

bool raise()

{

printf("\n请输入你需要的 *** 作代号:\n");

printf("\t1录入:完成抽奖者信息的输入;\n");

printf("\t2删除:完成抽奖者信息的删除;\n");

printf("\t3修改:允许对已经录入的数据重新进行编辑、修改;\n");

printf("\t4抽奖:开始抽奖!\n");

printf("\t5显示:显示所有中奖者的信息;\n");

printf("\t6退出。\n");

printf("==>");

int operate;

fflush(stdin);

scanf("%d",&operate);

switch(operate)

{

case 1:

info_login();

break;

case 2:

info_delete();

break;

case 3:

info_modify();

break;

case 4:

award();

break;

case 5:

info_show();

break;

case 6:

return false;

default:

break;

}

printf("\n *** 作完成。\n");

return true;

}

void main()

{

while(raise());

}

用datatxt文件保存以下内容:

13725528132 李桂荣

13725528131 李二来

13725528133 张荣刚

13725528130 荣南

13725528137 王三

13725528138 吴立

13725528139 郭德纲

13725528140 周星驰

13725528141 张曼玉

13725528142 张艺谋

13725528152 秦香莲

13725528162 潘金莲

13725528172 李大嘴

13725528182 展堂

//源代码如下

#include <stdioh>

#include <stdlibh>

#include <timeh>

#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("datatxt", "r");

if (fp == NULL)

{

printf("打开文件datatxt失败!\n");

return;

}

//当文件不为空

while (!feof(fp))

{

num ++;

fscanf(fp, "%s", persontelno);

fscanf(fp, "%s", personname);

personaward = '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;

}

以上就是关于如何使用javascript做一个简单的抽奖程序全部的内容,包括:如何使用javascript做一个简单的抽奖程序、vb6.0怎么制作一个抽奖小程序 vb6.0抽奖器的制作方法、VB设计一个随机抽奖程序,当单击开始按钮时,在两个文本框产生【1,20】之间的随机整数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9487937.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存