gpt4 book ai didi

c# - 如何将 int 转换为枚举值?

转载 作者:可可西里 更新时间:2023-11-01 09:14:22 27 4
gpt4 key购买 nike

<分区>

这是我的代码,它抛出一个错误,提示 Cannot convert type "int"to Cards.Suits无法将类型“int”转换为 Cards.Rank

private Card[] cards;
public Deck()
{
cards = new Card[52];
for (int suitVal = 0; suitVal < 4; suitVal++)
{
for (int rankVal = 0; rankVal < 14; rankVal++)
{
cards[suitVal * 13 + rankVal - 1] = new Card((Suits)suitVal, (Rank)rankVal);
}
}
}

卡片构造函数是

public readonly Suits suit;
public readonly Rank rank;
public Card(Suits newSuit, Rank newRank)
{
suit = newSuit;
rank = newRank;
}

现在 Suits 枚举和 Rank 枚举就像一副普通的纸牌,从 ACE = 1 开始,依此类推,花色是方 block 、梅花、红心、黑桃。谁能告诉我为什么我会收到上述错误。以下代码摘自一本书。谢谢!

*编辑

    public enum ranks
{
ACE = 1,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING,
}

public enum Suit
{
DIAMOND,
CLUB,
HEART,
SPADE,
}

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com