C# 简单生成双色球代码

红球篮球产生代码

public class TestD
    {
     //产生几组数据
        public void getzhu( int num)
        {
            for (int m = 0; m < num; m++)
            {
                getred();
                getblue();
               Console.WriteLine(""+(m+1).ToString()+"组数据红球:  "+ToString());
               red = new int[6];
               i = 0;
               b = 0;
               blue = new int[1];
            }
        }

        int[] red = new int[6];//6个红球
        int[] blue = new int[1];//一个篮球
        int i = 0;//红球数组索引
        int b = 0;//篮球数组索引
        //生成红球
        public void getred() {
            bool flag = true;
            do
            {
                int temp;
                temp = getnum(flag,33);
                if (red.Contains(temp))
                {
                    flag = true;
                }
                else {
                    red[i] = temp;
                    i++;
                    if (i > 5)
                    {
                        flag = false;
                    }
                    else
                    {
                        flag = true;
                    }
                }

            } while (flag);
           
        }
        //生成蓝球
        public void getblue()
        {
            bool flag = true;
            do
            {
                int temp;
                temp = getnum(flag, 17);
                if (blue.Contains(temp))
                {
                    flag = true;
                }
                else
                {
                    blue[b] = temp;
                    b++;
                    if (b > 0)
                    {
                        flag = false;
                    }
                    else
                    {
                        flag = true;
                    }
                }

            } while (flag);

        }
        //产生数字
        private int getnum(bool ifreset,int zuida)
        {   int inum=0;
            if (ifreset==true)
            {
            Random rd = new Random();
            inum = rd.Next(1, zuida);

            } 
            return inum;
        }
        //重写tostring方法
        public override string ToString()
        {
            return red[0].ToString() + "--" + red[1].ToString() + "--" + red[2].ToString() + "--" + red[3].ToString() + "--" + red[4].ToString() + "--" + red[5].ToString() + "   篮球是:" + blue[0].ToString();
        }

    }

 

调用代码

//调用代码
      TestD testd = new TestD();
       testd.getzhu(3);//生成三组

 

结果图

posted @ 2015-09-02 17:49  山水中华  阅读(2474)  评论(1编辑  收藏  举报