正如标题所说,我正在尝试使用枚举,但我得到了完全错误的输出。我没有得到卡片套装和等级,而是得到两个空值。
有人指出我正确的方向吗?
卡类
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());
}
}