我正在努力将在ENUMS中声明的值加在一起。我基本上需要根据等级将一手牌中的每张牌的值相加。

我已经尝试了一些东西,但实际上没有任何地方。我可以显示ENUM,但它们没有任何价值。

这是我的代码:

卡类

 public class Card {

 private Suit suit;
 private Rank rank;
 int totalValue;


 public Card (Suit suit, Rank rank) {
 this.suit = suit;
 this.rank = rank;
 }

public Suit getSuit(){
return suit;
}

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

public Rank getRank()
{
return rank;
}

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


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

 }


西装类

public enum Suit {

HEARTS , SPADES , CLUBS , DIAMONDS

}


等级等级

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 RankValue;


private Rank (int rValue)
{
RankValue = rValue;
}



public int getRankValue()
{
return RankValue;
}

}


手班

public class Hand {



private Card theCards[ ];
private int numCards;
private int totalValue;
private static final int max = 5;
private Rank rank;
private Suit suit;

public Hand ( )
{
 theCards = new Card [max];
 numCards = 0;
}

public void addCard( Card aCard )
{
     if (numCards < max) theCards[numCards++] = aCard;
}

public void setRank(Rank rank){

    this.rank = rank;

}

public void setSuit(Suit suit){

    this.suit = suit;
}

public int totalValue() {
int totalValue = 0;
for (Card card : theCards) {
totalValue += card.getRank().getRankValue();

}
return totalValue;

}

@Override
public String toString ( )
{
   String s = "Rank: " + rank + "Suit: " + suit;
   for (int i = 0; i < numCards; ++i)  {
    s += "\n" + theCards[i] + "\n" + totalValue;
   }

   return s;
}
}


最后是我的手驱动器的主要方法

public class HandDriver {

public static void main( String[] args ){

Hand h1 = new Hand();

Card c1 = new Card(Suit.valueOf("HEARTS"),Rank.valueOf("TEN"));
Card c2 = new Card(Suit.valueOf("SPADES"),Rank.valueOf("ACE"));
Card c3 = new Card(Suit.valueOf("DIAMONDS"),Rank.valueOf("SEVEN"));
Card c4 = new Card(Suit.valueOf("HEARTS"),Rank.valueOf("NINE"));
Card c5 = new Card(Suit.valueOf("HEARTS"),Rank.valueOf("THREE"));

h1.addCard(c1);
h1.addCard(c2);
h1.addCard(c3);
h1.addCard(c4);
h1.addCard(c5);


System.out.print (h1.toString());


;
}
}


我基本上是在寻找能够指出正确方向以显示所有卡总值的人

最佳答案

在您的Hand类中,您使用的是int totalValue,然后在函数totalValue()中将其“隐藏”,因此totalValue将为零。然后,在toString()中,您将使用永不计算的totalValue。首先,在此处删除“ int”:

public int totalValue() {
    //removed "int", so you are not declaring local totalValue variable
    //but using your class variable
    totalValue = 0;
    for (Card card : theCards) {
        totalValue += card.getRank().getRankValue();

    }
    return totalValue;
}


然后,在您的toString()中,调用上面的函数,以便计算totalValue,然后可以在循环中使用它:

@Override
public String toString ( )
{
    String s = "Rank: " + rank + "Suit: " + suit;

    //call totalValue() to calculate totalValue
    totalValue();

    for (int i = 0; i < numCards; ++i)  {
        //now you can use totalValue, because it's been calculated in totalValue()
        s += "\n" + theCards[i] + "\n" + totalValue;
    }

    return s;
}


正如Holger所注意到的,您还可以使用Suit.HEARTS代替Suit.valueOf(“ HEARTS”)。

编辑:正如格罗夫所注意到的,不必为每张卡都调用totalValue()。在构建字符串之前先调用它,然后可以在for循环中使用totalValue变量。

10-08 16:39