C#我有50多个其他if卡的声明,有没有办法缩短它或一次性完成所有操作?

Tay*_*lor 6 c# if-statement

我有一个加载并生成7个不同随机数的表单,从1到13,1是Ace,13是King.在生成7个不同的随机数后,它将每个随机数放入7个图像框中.我正在使用if语句显示图片框.

它还循环通过一系列"黑桃,心,俱乐部和钻石",13次.

我的if语句如下:

if (cardNum == 1 && cardType ==  "Spades")
{
    pictureBox1.Image = ace_of_spades;
}
else if (cardNum == 1 && cardType == "Hearts")
{
    pictureBox1.Image = ace_of_hearts;
}
else if (...)
{
    //change picture box
} //repeat it like 50 times
Run Code Online (Sandbox Code Playgroud)

是否有一种简单,容易的方法来挑选7张随机卡并在图片框中显示它们?

这是非常耗时的,就像我这样做.

byt*_*e77 5

有很多方法,但总的来说我会说:使用数组作为查找

例如,你有一个像这样的数组

Image[] images = new[]
{
    ace_of_spades,
    ace_of_hearts,
    ...
};
Run Code Online (Sandbox Code Playgroud)

现在你所要做的就是计算正确的指数.由于您没有提供我需要的尽可能多的信息来帮助您,我只是猜测它看起来与此类似:

pictureBox1.Image = images[cardNum * 7 + (int)cardType];
Run Code Online (Sandbox Code Playgroud)

就像我说的,这就是背后的想法.现在你必须找到合适的计算方法.


Mik*_*e U 4

关于如何处理这个问题有很多非 OOP 建议。这是我的解决方案,它让每个对象跟踪自身,并将提供一种简单的方法来洗牌并获取与每张纸牌相关的图像(我已经编写了一些纸牌游戏)。

为了存储实际图像,请将它们作为资源文件嵌入到您的项目中,并以特定方式命名:

  • 卡_俱乐部_1
  • 卡_俱乐部_2
  • 卡_俱乐部_3
  • ETC...

然后,当您需要卡牌图像时,只需组合花色和值并向资源管理器请求该资源名称,如下所示。此方法需要更多的设置和规划,但会给您带来清晰的代码。您甚至可以在一个单独的项目中完成所有这些工作,然后通过在您想要使用一副纸牌的应用程序中引用 DLL 来重新使用这些类/资源。

enum Suit : uint
{
    Club = 0,
    Heart,
    Spade,
    Diamond
}
class Card
{
    public int
        Value;
    public Suit
        Suit;

    public System.Drawing.Image GetImage()
    {
        return System.Drawing.Image.FromStream(
            global::cardLibraryProject.Properties.Resources.ResourceManager.GetStream(string.Format("card_{0}_{1}", this.Suit, this.Value))
        );
    }
}
class Deck
{
    System.Collections.ArrayList
        _arr;

    private Deck()
    {
        this._arr = new System.Collections.ArrayList(52);
    }

    void Add(Card crd)
    {
        if (!this._arr.Contains(crd))
            this._arr.Add(crd);
    }

    public void Shuffle()
    {
        Random rnd = new Random(DateTime.Now.Millisecond);
        System.Collections.ArrayList tmp1 = new System.Collections.ArrayList(this._arr);
        System.Collections.ArrayList tmp2 = new System.Collections.ArrayList(52);
        while (tmp1.Count > 0)
        {
            int idx = rnd.Next(tmp1.Count);
            tmp2.Add(tmp1[idx]);
            tmp1.RemoveAt(idx);
        }
        this._arr = tmp2;
        tmp1.Clear();
        tmp2.Clear();
    }

    public static Deck CreateDeck()
    {
        Deck newDeck = new Deck();
        for (int s = 0; s < 4; s++)
            for (int i = 0; i < 13; i++)
                newDeck.Add(new Card { Value = i, Suit = (Suit)s });
        return newDeck;
    }
}
class Program
{
    public void Main(string[] args)
    {
        Deck cards = Deck.CreateDeck();
        cards.Shuffle();

        pictureBox1.Image = cards[0].GetImage();
        // code to play game would go here.  Obviously, if you took
        // my suggestion about creating a "Cards" library, then you
        // wouldn't have a "void Main" at all, and this would
        // all go in the application that was the actual game.
    }
}
Run Code Online (Sandbox Code Playgroud)