刚学C#不久。但是不知道为什么运行程序得不到正确结果。
这个程序主要是从谁机52张牌里谁机取5张。其中getfivecard()这个函数就是谁机取5张牌如果把断点设到这个函数里面可以得到5张不同结果的牌,但是如果把断点设在这个函数的后面就只能得到一种牌。而 private int[] fivecards = new int[5];得到的数组也是5个同样的数。我不知道怎么搞的。弄了很久也不知道怎么搞的。现在想请教大家
这是我的程序
using System;
using System.Collections.Generic;
using System.Text;
using ClassLib;namespace _5
{
    public class Program
    {
        private int[] fivecards = new int[5];
        static void Main(string[] args)
        {
            Deck mydeck = new Deck();
            Program mypro = new Program();
            mydeck.Shuffle();            mypro.getfivecard();
            for (int i = 0; i < 5; i++)
            {
                int temp = mypro.fivecards[i];
                Crad mycard = mydeck.GetCard(temp);
                //mydeck.getcard(temp);
                Console.WriteLine(mycard.ToString());            }        }
        public void getfivecard()
        {            bool[] mybool = new bool[52];            for (int i = 0; i < 5; i++)
            {
                int destcard = 0;
                bool assgined = false;                Random r = new Random();
                while (assgined == false)
                {                    destcard = r.Next(52);
                    if (assgined == false)
                    {
                        fivecards[i] = destcard;
                        assgined = true;
                    }
                    mybool[destcard] = true;
                }
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;namespace ClassLib
{
    public enum suit
    {
        Club,
        Diamond,
        Heart,
        Spade
    }
    public enum Rank
    {
        Ace = 1,
        Deuce,
        Three,
        Four,
        Five,
        Six,
        Seven,
        Eight,
        Nine,
        Ten,
        Jack,
        Queen,
        King    }
    public class Crad
    {
        public readonly suit suit;
        public readonly Rank rank;
        public override string ToString()
        {
            return "The" + rank + "of" + suit + "s";
        }
        private Crad()
        {        }
        public Crad(suit newSuit, Rank newRank)
        {
            suit = newSuit;
            rank = newRank;
        }    }
}
using System;
using System.Collections.Generic;
using System.Text;namespace ClassLib
{
    public class Deck
    {
        private Crad[] cards;
        public Deck()
        {
            cards = new Crad[52];
            for (int suitVal = 0; suitVal < 4; suitVal++)
            {
                for (int rankVal = 1; rankVal < 14; rankVal++)
                {
                    cards[suitVal * 13 + rankVal - 1] = new Crad((suit)suitVal, (Rank)rankVal);
                }
            }
        }
        public Crad GetCard(int cardnum)
        {
            if (cardnum >= 0 && cardnum <= 51)
                return cards[cardnum];
            else
                throw (new System.ArgumentOutOfRangeException("cardnum", cardnum, "值必须在1到51之间"));
        }
        public void Shuffle()
        {
            Crad[] newDeck = new Crad[52];
            bool[] assigned = new bool[52];
            for (int i = 0; i < 52; i++)
            {
                int destCard = 0;
                bool foundCard = false;
                Random sourceGen = new Random();
                while (foundCard == false)
                {
                    destCard = sourceGen.Next(52);
                    if (assigned[destCard] == false)
                        foundCard = true;
                }
                assigned[destCard] = true;
                newDeck[destCard] = cards[i];
            }
            cards = newDeck;
        }    }
}
另外碰到这中问题有好的调试方法没有
请大家看看谢谢。

解决方案 »

  1.   


                Random r = new Random();        //<----
                for (int i = 0; i < 5; i++) 
                { 
                    int destcard = 0; 
                    bool assgined = false;                 //Random r = new Random(); 
                    while (assgined == false) 
                    {                     destcard = r.Next(52); 
                        if (assgined == false) 
                        { 
                            fivecards[i] = destcard; 
                            assgined = true; 
                        } 
                        mybool[destcard] = true; 
                    } 
                } 
      

  2.   


     有断点就有一个数呀,运行一个后就中断了(暂停) 而你的 private int[] fivecards = new int[5]; 这一句话。在 new 的时候, 就自动在内存里分配了5个int的空间了。即使你一个也得不到。这个数组照样还是 5 个
      

  3.   

    Random的默认构造函数用当前时间作为随机种子,
    在一个循环中不断构造Random,它们的随机种子可能是一样的,造成r.Next()得到一样的值。
    而当你观察断点后,人为地延长了两次构造Random类之间的间隔,使得r.Next()得到不一样的值。解决方法就是把Random的构造发到循环外面。
      

  4.   

    我照楼上说的办了可以新的问题出来了 
    1  为什么我的取52张牌的时候的函数Shuffle() 在for里面去照样可以得到52个打乱的数呢。并且出的牌也不同
    2.还有就是找上说的把它放到for外面但是为什么得到的5张牌是同一种花色的而且都是CLUB这个色的很怪.
      

  5.   


    以下代码供你参考(其中的洗牌是稳定的O(n)算法):// initialize 52 cards
    int[] cards = new int[52];
    for (int i = 0; i < cards.Length; i++)
    {
        cards[i] = i;
    }// shuffle
    Random random = new Random();
    for (int i = cards.Length - 1; i > 0; i--)
    {
        int j = random.Next(i);
        int swap = cards[i];
        cards[i] = cards[j];
        cards[j] = swap;
    }// print the result
    string[] suits = { "Spade", "Heart", "Diamond", "Club"};
    for (int i = 0; i < cards.Length; i++)
    {
        int suit = cards[i] / 13;
        int rank = cards[i] % 13 + 1;    Console.WriteLine(suits[ suit ] + " " + rank);
    }