首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

号码抽奖系统小疑点

2013-12-16 
号码抽奖系统小问题本帖最后由 bulls5988 于 2013-12-11 15:04:14 编辑要做一个号码抽奖的小程序,为不可放

号码抽奖系统小问题
本帖最后由 bulls5988 于 2013-12-11 15:04:14 编辑 要做一个号码抽奖的小程序,为不可放回的抽奖(中的号码不会再出现);输入一个数字比如100,放在一张表里一个字段:100 。然后用随机函数把这个100做参数来随机,中一个号码写入一张表里。在表中有的号码就不允许在被抽中了。我想用timer做现在逻辑上有点问题。


  private void start_Click(object sender, EventArgs e)
        {

            //开始抽奖
            if (timer1.Enabled == false)
            {
                timer1.Enabled = true;
            }
            else
            {
                //这步应该做判断,数据库中表中是否存在号码,如果有不允许被抽取
                
//保存抽取结果
                luck_update = "insert into zj_hm (zj_number) values 
('" + label1.Text + "')";
                SqlConnection conn_update = new SqlConnection(my_conn.sql_conn());
                SqlCommand cmd_update = new SqlCommand(luck_update, conn_update);
                conn_update.Open();
                cmd_update.ExecuteNonQuery();
                timer1.Enabled = false;
            }
        }

//计时器Timer
  private void timer1_Tick(object sender, EventArgs e)
        {
            //在表中抽奖号段范围内抽取随机号;开始号码,截至号码
            luck = "select * from cj_hdnr";
            SqlConnection conn = new SqlConnection(my_conn.sql_conn());
            SqlCommand cmd = new SqlCommand(luck,conn);
            conn.Open();
            SqlDataReader rs = cmd.ExecuteReader();
            if(rs.Read())
            {
                start = Convert.ToInt32(rs["hd_kaishi_haoma"]);
                end = Convert.ToInt32(rs["hd_jieshu_haoma"]);
            }
            conn.Close();
            rs.Close();       
            Random rnd = new Random();     
            label1.Text = (rnd.Next(start,end)).ToString();
        }

[解决办法]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            RandNum a = new RandNum();


            a.UI();

            Console.Read();
        }
    }

    public class RandNum
    {
        public List<int> ExNum { get; set; }//已经抽中的数
        public List<int> TotalNum { get; set; }
        private int count; //范围
        private int SELCOUNT;
        private int selCount;//选几个

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ct">大于零的整数</param>
        public RandNum()
        {
            ExNum = new List<int>();
            TotalNum = new List<int>();
        }

        private int GetCurrentCount()
        {
            lock (this)
            {
                return selCount;
            }
        }

        private void Run()
        {
            Random r = new Random();
            do
            {
                int num = 0;
                int i = r.Next(TotalNum.Count);
                num = TotalNum[i];
                ExNum.Add(num);
                TotalNum.RemoveAt(i);
                System.Threading.Thread.Sleep(10000);//10秒中抽一个出来
                Console.WriteLine("\r第{0}个性欲号码已产生:{1}", SELCOUNT - GetCurrentCount()+1, num);
            } while (Interlocked.Decrement(ref selCount) > 0);
        }

        public void UI()
        {
            while (true)
            {
                Console.WriteLine("===========YY抽奖程序===========");
                Console.WriteLine("请输入要抽奖的总数(从0开始编号)");
                string s = Console.ReadLine();

                int ct = Convert.ToInt32(s);
                if (ct <= 0)
                    Console.WriteLine("输入大于零的整数");
                else
                {
                    Console.WriteLine("请输入要从中选从少个数");
                    string s1 = Console.ReadLine();
                    int ct1 = Convert.ToInt32(s1);
                    if (ct1 <= 0)
                        Console.WriteLine("输入大于零的整数");



                    if (ct1 > ct)
                        Console.WriteLine("输入数目不能大于总数");
                    else
                    {
                        count = ct;
                        selCount = ct1;
                        SELCOUNT = ct1;
                        while (ct-- > 0)
                        {
                            TotalNum.Add(ct);
                        }
                        break;
                    }
                }
            }

            //开始计算
            Console.WriteLine("开始抽奖...");
            Task.Factory.StartNew(Run);
            Random r = new Random();
            while (true)
            {
                Console.Write(r.Next(count));
                System.Threading.Thread.Sleep(500);
                Console.Write("\r");
                Console.Write("    \r");

                if (GetCurrentCount() <= 0)
                {
                    Console.WriteLine("抽奖结束");
                    Console.WriteLine("中奖序列为:");
                    ExNum.ForEach(Console.WriteLine);

                    break;
                }
            }
        }
    }
}



楼主我写了一个控制台的抽奖程序,你可以复用,只是把ui方法修改一下就可以了.

热点排行