扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
在窗体上添加一个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
语言实现此程序。
以下是VB编程语言的实现示例:
'定义变量,存储每个奖项的人数
Dim firstPrize As Integer = 3
Dim secondPrize As Integer = 5
Dim thirdPrize As Integer = 12
Dim souvenirPrize As Integer = 10
'定义变量,存储抽奖结果
Dim firstPrizeList As New List(Of Integer)
Dim secondPrizeList As New List(Of Integer)
Dim thirdPrizeList As New List(Of Integer)
Dim souvenirPrizeList As New List(Of Integer)
'定义变量,存储编号滚动的起始编号
Dim currentNum As Integer = 1
'循环抽取各种奖项
For i As Integer = 1 To firstPrize
'抽取一等奖,并将编号添加到一等奖名单中
firstPrizeList.Add(currentNum)
currentNum += 1
Next
For i As Integer = 1 To secondPrize
'抽取二等奖,并将编号添加到二等奖名单中
secondPrizeList.Add(currentNum)
currentNum += 1
Next
For i As Integer = 1 To thirdPrize
'抽取三等奖,并将编号添加到三等奖名单中
thirdPrizeList.Add(currentNum)
currentNum += 1
Next
For i As Integer = 1 To souvenirPrize
'抽取纪念奖,并将编号添加到纪念奖名单中
souvenirPrizeList.Add(currentNum)
currentNum += 1
Next
'输出抽奖结果
Console.WriteLine("一等奖名单:" String.Join(",", firstPrizeList))
Console.WriteLine("二等奖名单:" String.Join(",", secondPrizeList))
Console.WriteLine("三等奖名单:" String.Join(",", thirdPrizeList))
Console.WriteLine("纪念奖名单:" String.Join(",", souvenirPrizeList))
我用多线程写的,你可以参考下。
系统界面图如下:(注:重置功能为清除已经中奖号码,回归1-100都可以中奖状态。)
代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
System.Threading.Thread g_Thread = null;
Listint g_list = new Listint();
int g_Num = 0;
delegate void g_delegate();
/// summary
/// 开始
/// /summary
/// param name="sender"/param
/// param name="e"/param
private void button2_Click(object sender, EventArgs e)
{
if(g_list.Count==100)
{
MessageBox.Show("所有数字已使用,请重置!");
return;
}
button3.Enabled = true;
Random r = new Random();
g_delegate dele= ChangeTxt;
g_Thread = new System.Threading.Thread(() =
{
while(this.IsDisposed==false)
{
g_Num = r.Next(1, 100);
if(g_list.Contains(g_Num))
{
g_Num = 1;
}
while(g_list.Contains(g_Num))
{
g_Num++;
}
this.Invoke(dele);
System.Threading.Thread.Sleep(50);
}
});
g_Thread.Start();
}
void ChangeTxt()
{
label2.Text = g_Num.ToString();
}
/// summary
/// 停止
/// /summary
/// param name="sender"/param
/// param name="e"/param
private void button3_Click(object sender, EventArgs e)
{
//if(g_Thread.ThreadState==System.Threading.ThreadState.Running)
{
g_Thread.Abort();
g_list.Add(Convert.ToInt32(label2.Text));
}
button3.Enabled = false;
}
/// summary
/// 重置
/// /summary
/// param name="sender"/param
/// param name="e"/param
private void button1_Click(object sender, EventArgs e)
{
g_list.Clear();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
System.Environment.Exit(0);
}
}
把你的代码发出来。
你这种方式的程序只能弄一等奖吧,为什么会有二等奖三等奖?
获奖次数跟电话号码什么关系,为什么不能重复?同一个号码不能多次获奖?那就不是重复的问题,你的提问有些不清不楚。
暂定的解决方案——
把随机生成的电话号码,赋值给公共数组,如果公共数组中存在新生成的电话号码,那么重新生成电话号码,如果电话号码不在数组中存在,那么将电话号码加入到数组中。重复以上过程,直至喊停。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流