• 摇奖


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;//引入动态数组的命名空间,因为要用到动态数组去存放label上的值
    using System.Threading;//引入线程的命名空间

    namespace Ball_1
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    Thread playBallThread;//定义线程
    public static ArrayList al = new ArrayList();//动态数组用来接收数字

    private void button1_Click(object sender, EventArgs e)
    {
    al = new ArrayList();//重新定义动态数组的值
    playBallThread = new Thread(BallThread);//让线程执行BallThread方法
    playBallThread.Start();//执行线程
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    CheckForIllegalCrossThreadCalls = false;//表示不安正常的步骤来进行运用线程
    }

    public void BallThread()//自定义一个方法:传进是那个标签
    {
    Ball b1 = new Ball(label1, BallType.Red);//实例化ball类,因为ball自定义了构造函数所以这里就要用到参数的,将这里label1,BallType.Red的赋值给字段
    Thread t1 = new Thread(b1.numeber);//实例化线程,让线程调用ball类中的方法
    t1.Start();//执行线程
    Thread.Sleep(1000);//让线程“休息”一秒

    Ball b2 = new Ball(label2, BallType.Red);
    Thread t2 = new Thread(b2.numeber);
    t2.Start();
    Thread.Sleep(1000);

    Ball b3 = new Ball(label3, BallType.Red);
    Thread t3 = new Thread(b3.numeber);
    t3.Start();
    Thread.Sleep(1000);

    Ball b4 = new Ball(label4, BallType.Red);
    Thread t4 = new Thread(b4.numeber);
    t4.Start();
    Thread.Sleep(1000);

    Ball b5 = new Ball(label5, BallType.Red);
    Thread t5 = new Thread(b5.numeber);
    t5.Start();
    Thread.Sleep(1000);

    Ball b6 = new Ball(label6, BallType.Red);
    Thread t6 = new Thread(b6.numeber);
    t6.Start();
    Thread.Sleep(1000);

    al.Sort();//数组的排序

    label7.Text = string.Format("中奖号码是:{0} {1} {2} {3} {4} +{5}",al[0],al[1],al[2],al[3],al[4],label6.Text);//将在球上面显示的值进行排序,然后在标签上显示出来,因为篮球不用排序所以就要将篮球取出在排序之外的
    }
    }

    public enum BallType { Red,Blue}//定义一个枚举,用来表示球的颜色:红 蓝(实质是一个来类)
    class Ball//定义一个球类
    {
    Label ballLabel;//定义字段(这里是用一个类的实例来作为字段的)
    BallType type;

    public Ball(Label lb, BallType bt)//定义构造函数,参数是两个类,用类的实例来作为变量
    {
    ballLabel=lb;//将lb赋值给字段
    type = bt;//将bt的值赋值给字段
    }

    Random r = new Random();//定义随机数
    public void numeber()//自定义一个方法:用来判断指数否被加进label
    {
    int num;//定义一个变量,用来接收随机数
    if (type == BallType.Red)//判断如果type的值是红,也就是红球时
    {
    do
    num = r.Next(1, 34);
    while (Form1.al.Contains(num));
    Form1.al.Add(num);
    //这里用do...while循环是用到其特点:首先要确定数已经被加进ArrayList里面,在while中判断下一个要加进ArrayList里面的数是否已经在求上面显示了,也就是这里所说的“包含”,如果没有就将其加进ArrayList里面
    }
    else//这里的是:专门针对篮球的
    {
    num = r.Next(1, 17);
    }
    ballLabel.Text = num.ToString();//这里是:将随机数存放到label上面
    //这里的ballLabel是ball类中的字段
    }
    }
    }

  • 相关阅读:
    迷宫最短路径问题的dfs,bfs实现
    高效率的排序算法2--归并排序
    高效率的排序算法1--快速排序
    POJ No.2386 Lake Counting
    部分和问题 --dfs
    dfs的一个小实现(啊哈算法的例题)
    HDU 5710 Digit Sum
    【视频播放器】在macOS上使用IINA
    视音频格式基础知识&视频压缩
    影视作品(电影,电视剧,PV,MAD,AMV)的制作程序
  • 原文地址:https://www.cnblogs.com/meroselove/p/1828392.html
Copyright © 2020-2023  润新知