当前位置:编程学习 > C#/ASP.NET >>

单步执行结果输出正确,但执行的时候没结果输出。

从52张扑克牌中随机抽出5张,然后将5张牌的名字打印。求教了


        static void Main(string[] args)
        {
            Deck myDeck = new Deck();
            myDeck.Shufle();                //洗牌,即打乱牌的顺序

            Card[] selectedCards = new Card[5];
            int[] coCards = new int[5];
            for (int i = 0; i < 5;i++ )
            {
                coCards[i] = i;             //初始化
            }

            //随机抽取0到51之间的5个不重复的数字
            for (int i = 0; i < 5;i++ )
            {
                //检查该数字是否存在于coCards数组
                int tempNum = 0;
                Boolean exist = false;
                Random tempRandom = new Random();

                do 
                {
                    tempNum=tempRandom.Next(0,52);
                    for (int j=0;j<i;j++)
                    {
                        if (coCards[j]==coCards[i])
                        {
                            exist=true;
                        }
                        else
                        {
                            exist = false;
                        }
                    }
                } while (exist==true);

                if (exist==false)
                {
                    coCards[i]=tempNum;
                }
            }

            //把扑克牌里对应的5张牌取出来
            for (int i = 0; i < 5;i++ )
            {
                selectedCards[i] = myDeck.GetCard(coCards[i]);
            }

            //显示5张牌
            for (int i = 0; i < 5;i++ )
            {
                Console.WriteLine("{0}",selectedCards[i].ToString());
            }

            Console.ReadKey();
--------------------编程问答-------------------- Random tempRandom = new Random();放到for循环外面 --------------------编程问答--------------------

const Int32 length = 52;
        static void Main(string[] args)
        {
            Random r = new Random();

            //随即生成52个数。
            List<Int32> poker = new List<Int32>(length);
            for (Int32 i = 1; i <= length; i++)
                poker.Add(r.Next(100, 1000));

            while (poker.Count > length - 5)
            {
                int current = poker[r.Next(poker.Count)];
                Console.WriteLine(current);
                poker.Remove(current);

            }

            Console.ReadLine();
        }

哥们,不晓得为毛这么复杂啊。

--------------------编程问答-------------------- 先把随机的弄好在说!!! --------------------编程问答-------------------- 问题就是出现在随机数那里,程序执行太快,随机数根本不会变化 。
--------------------编程问答--------------------

        public static int[] GetRandom2(int minValue, int maxValue, int count)
        {
            int[] intList = new int[maxValue];
            for (int i = 0; i < maxValue; i++)
            {
                intList[i] = i + minValue;
            }
            int[] intRet = new int[count];
            int n = maxValue;
            Random rand = new Random();
            for (int i = 0; i < count; i++)
            {
                int index = rand.Next(0, n);
                intRet[i] = intList[index];
                intList[index] = intList[--n];
            }


刚才从网上搜索到的一段算法。这最后一句intList[index] = intList[--n];是什么意思呢?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,