【问题标题】:How to convert int to enum value? [duplicate]如何将int转换为枚举值? [复制]
【发布时间】:2013-08-03 06:43:56
【问题描述】:

这是我的代码,它抛出了一个错误,说 Cannot convert type "int" to Cards.SuitsCannot convert type "int" to 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 开始的常规牌组,以此类推,花色是 DIAMONDS、CLUBS、HEARTS、SPADES。 谁能告诉我为什么我得到上述错误。以下代码取自一本书。 谢谢!

*编辑

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

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

【问题讨论】:

  • 你能给出SuitsRank的定义吗?
  • 不是重复的,因为使用了该方法并且仍然返回错误
  • 您使用的是(Suits)suitVal,但您发布的枚举实际上名为Suit
  • 您的代码定义了枚举ranksSuit,但尝试转换为RankSuits 类型。确保所有类型名称都匹配。
  • Nope that doesn't work Error 1 找不到类型或命名空间名称“Suit”(您是否缺少 using 指令或程序集引用?)

标签: c# class


【解决方案1】:

根据您的枚举声明,Suit 在 [0..3] 范围内,而排名在 [1..13] 范围内(注意,排名不是从零开始的),所以内部 for 循环应该更正:

  for (int rankVal = 0; rankVal < 13; rankVal++) // <- 14 changed for 13: [0..13] has the same length as [1..14] 
  { 
    cards[suitVal * 13 + rankVal] = new Card((Suits)suitVal, (Rank)(rankVal + 1)); // <- removed -1 from index; add 1 to rankVal, we need [1..14], not [0..13]
    ...

【讨论】:

  • -1 因为当 suitval 和 ranval 都为 0 时,上面的代码会抛出错误。这会将这张卡 [suitVal * 13 + rankVal - 1] 评估为卡 [- 1]。因此索引超出范围。
  • @Ehsan Ullah:谢谢,我已经更正了卡片中的索引[…]
【解决方案2】:

像这样改变你的线路

 cards[suitVal * 13 + rankVal] = new Card(((Suit)suitVal), ((ranks)rankVal));

并且由于您的类在构造函数中使用枚举,因此请像这样更改它

public readonly Suit suit;
public readonly ranks rank;
public Card(Suit newSuit, ranks newRank)
{
    suit = newSuit;
    rank = newRank;
}

【讨论】:

  • Intellisense 仍在指导我使用公共只读西装套装;反对你的回答。我应该提到这是一个类库。
  • 我已将西装套装更新为西装套装;您将枚举作为参数传递,即 Suit(在您上面给出的代码中)。不是西装。
  • 这在我不得不这样做时不起作用,它是 Suits.Suit 和 Ranks.rank
  • @user2434321 请发布您的整个代码。什么是西装?是一门课吗?什么是排名?我们不能一直猜测
【解决方案3】:

我会看到它会得到其他错误“索引超出了数组的范围”

当 suitVal = 0 且 rankVal = 0 时,cards[suitVal * 13 + rankVal - 1] =-1,在数组索引之外。

【讨论】:

    【解决方案4】:

    这运行良好 但更正一下

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

    这里

    在牌[suitVal * 13 + rankVal - 1]

    [Loop 1:]
    suitVal =0
    rankVal=0
    

    [suitVal * 13 + rankVal - 1]= [0*13+0-1]= [-1] !哎呀!

    在您的计划中:

    public readonly Suit  suit; //not Suits
    public readonly ranks rank; //not Rank 
    

    【讨论】:

      猜你喜欢
      • 2011-12-30
      • 1970-01-01
      • 2010-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多