我目前正在對使用枚舉卡類的手,要求我補充方法是:手牌:總價值(JAVA)
添加卡進手
計算總價值所有牌在手中(所以如果手中有3個10,那麼總價值將是30)
然而,問題是我無法找到如何將所有總數相加在手階層中使用該方法(如任務要求我們)。
任何幫助,將不勝感激
繼承人的代碼,我的西裝枚舉
public enum Suit
{ HEARTS, CLUBS, DIAMONDS, SPADES; }
這裏是我的排名enum.This代碼一個有幾個增加的方法,包括getValue方法
public enum Rank {
ONE(1),TWO(2),THREE(3),FOUR(4),FIVE(5), SIX (6), SEVEN(7), EIGHT(8), NINE(9), TEN(10), KING(10), QUEEN(10),
JACK(10);
private final int cardValue;
private Rank (int nDays) {
cardValue = nDays;
}
public int getValue() {
return cardValue;
}
}
這是使用Rank和Suit枚舉來打印詳細信息(原諒混亂)的Card類
public class Card {
private Rank rank;
private Suit suit;
public Card (Rank theRank) {
this(theRank,Suit.HEARTS);
}
public Card (Rank theRank, Suit theSuit) {
rank = theRank;
suit = theSuit;
}
public Rank getRank() { return rank; }
public Suit getSuit () { return suit; }
public String toString () { return (rank + " of " + suit +"\n Value of card = " + rank.getValue()); }
}
這是手工類,需要addCard法和總價值的方法。我也做了addCard方法,現在需要做的總價值法)
public class Hand {
private Card theCards[ ];
private Rank rank;
private int numCards; private int totalValue;
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;
else
{
System.out.println("Cannot add any more cards as hand is full");
}
}
public void totalValue()
{
int handValue = 0;
for(Card C: theCards)
{
//get the rank values to appear then print them off
//Add up all values of cards and display the total
totalValue = handValue + C.getValue(); //This was just a test to see if calling up the value of card C would work
}
System.out.println("The total value of all the cards is: " + totalValue);
}
public String toString ()
{
String s = "";
for (int i = 0; i < numCards; ++i) { s += "\n" + theCards[i];
}
return s;
}
至於執行力度去其剛剛創建的卡對象,並將它們添加到對象的手的情況下,所以我不認爲我會做需要在這裏發佈。感謝任何能幫助我解決問題的人。
如果這是家庭作業,請將其標記爲家庭作業。 – erickson
女王和國王怎麼樣? – Gevorg
@Gevorg:向右滾動代碼窗口。 :o –