手牌:总价值(Java)

时间:2011-10-08 16:07:19

标签: java

我目前正在制作一个使用枚举的手牌类,需要我添加以下方法:

将卡片添加到手中

计算手中当前所有牌的总价值(如果手牌中有3个10,则总价值为30)

然而问题是我无法找到如何将所有总数加起来,同时仍然在手类中强调方法(正如任务要求我们)。

非常感谢任何帮助

以下是我的Suit枚举的代码

public enum Suit 
{    HEARTS, CLUBS, DIAMONDS, SPADES; }

这是我的Rank enum的代码。这个有一些添加的方法,包括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;
    }
}

这是Card类,它使用Rank和Suit枚举来打印细节(请原谅)#/ p>

 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方法和totalValue方法的Hand类。我已经完成了addCard方法,现在需要做totalValue方法)

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

就implimentation而言,它仅仅是创建卡片对象并将它们添加到Hand对象的情况,所以我认为我不需要在这里发布它。感谢能帮助我解决问题的任何人。

4 个答案:

答案 0 :(得分:2)

通过Card字段可以获得rank的值。现在,您正试图直接在getValue()上致电Card,但目前还没有这样的方法。

此外,您通常return来自totalValue()方法的手的总价值,而不是简单地打印它;这允许其他代码实际使用该值。

答案 1 :(得分:2)

卡没有getValue()方法,但有Rank。因此,请通过getRank()方法获取卡的排名,然后在返回的排名上调用getValue()

答案 2 :(得分:-1)

我设法弄清楚如何在不返回任何nullexception错误的情况下进行打印。基本上我只是将数组转换为arraylist,因为它更实用。这是Hand类的最终版本。

 import java.util.*;
 public class Hand {

 private ArrayList<Card> theCards = new ArrayList<Card >() ;
 private static final int max = 5;

 public void addCard( Card aCard )
 {
      if (theCards.size() < max)
              theCards.add(aCard);
      else
      {
          System.out.println("Cannot add any more cards as hand is ull");
      }
 }
 public void totalValue() {
 int totalValue = 0;
 for (Card card : theCards) {
     totalValue += card.getRank().getValue();

     }
  System.out
     .println("The total value of all the cards is: " + totalValue);
 }

    public void outputAllCards ( )
 {
        System.out.println("Outputting all card etails\n========================");
        for (Card card : theCards)
        {
            System.out.println(card.toString());
        }
        System.out.println("========================");
  } }

我输入了println,以便在调用totalValue方法时输出消息。我尝试通过将方法转换为字符串或int来返回但是这不起作用

答案 3 :(得分:-2)

该值在等级内。手中的排名对象是否扮演任何角色(例如手中相应牌的价值加倍,你必须指定游戏规则)?你只需要加上排名值。我重写了你的代码,省去了一些不必要的计数器/变量。另一方面,如果它可以增长,最好保留一个列表而不是数组。

import java.util.ArrayList;
import java.util.List;

public class Hand {
    private final List<Card> cards = new ArrayList<Card>();
    private Rank rank;
    private static final int MAX = 5;

    public void addCard(Card aCard) {
    if (cards.size() < MAX) {
        cards.add(aCard);
    } else {
        System.out.println("Cannot add any more cards as hand is full");
    }
    }

    public void totalValue() {
    int totalValue = 0;
    for (Card card : cards) {
        totalValue += card.getRank().getValue();

    }

    System.out
        .println("The total value of all the cards is: " + totalValue);
    }

    @Override
    public String toString() {
    StringBuilder sb = new StringBuilder();
    for (Card card : cards) {
        sb.append('\n').append(card.toString());
    }

    return sb.toString();

} }