0

正如标题所说,我正在尝试使用枚举,但我得到了完全错误的输出。我没有得到卡片套装和等级,而是得到两个空值。

有人指出我正确的方向吗?

卡类

public class Card {



    private Suit suit;
    private Rank rank;



    public Card (Suit suit, Rank rank) {

    }

    public Suit getSuit(){
        return suit;
    }

      public void setSuit(Suit suit)
    {
    this.suit = suit;
    }

    public Rank getRankValue()
    {
    return rank;
    }

    public void setRankValue(Rank rank)
    {
    this.rank = rank;
    }


    @Override
    public String toString ( ) { return ( rank + " of " + suit ); }

    }

手级

public class Hand {

private Card theCards[ ];
private int numCards;
private static final int max = 5;

public Hand ( )
{
 theCards = new Card [max];
 numCards = 0;
}
public void addCard( Card aCard ) 
{ 
     if (numCards < max) theCards[numCards++] = aCard; 
}
@Override
public String toString ( ) 
{
   String s = ""; 
   for (int i = 0; i < numCards; ++i)  { 
    s += "\n" + theCards[i]; 
   }
   return s;
}
}

等级等级

public enum Rank {

ACE(1),
TWO(2),
THREE(3),
FOUR(4),
FIVE(5),
SIX(6),
SEVEN(7),
EIGHT(8),
NINE(9),
TEN(10),
JACK(10),
QUEEN(10),
KING(10);

private int rank;

private Rank (int value)

{
 this.rank = value;
}

public int getRankValue() {

return rank;
}
}

西装类

public enum Suit {

HEARTS , SPADES , CLUBS , DIAMONDS 
}

还有我创建卡片和调用 toString 的类。

public class HandDriver {

public static void main( String[] args ){ 

Card c1 = new Card(Suit.valueOf("HEARTS"),Rank.valueOf("TEN"));
System.out.print (c1.toString());
}
}
4

1 回答 1

5
public Card (Suit suit, Rank rank) {

}

问题不在于枚举,而是在您的构造函数中,您实际上什么也没做。你得到SuitandRank作为参数,但你没有将它们保存在局部变量中。就像在下面的代码中一样,您需要使用通过构造函数获得的参数来分配局部实例变量的值。

public Card (Suit suit, Rank rank) {
   this suit = suit;
   this.rank = rank;
}
于 2013-11-08T16:13:12.463 回答