⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blackjackhand.java

📁 JAVA 2入门经典 练习答案
💻 JAVA
字号:
import java.util.Vector;
public class BlackjackHand
{
	// Add a card to the hand
	public BlackjackHand add(Card card) {
		hand.add(card);
		return this;
	}

  // Calculates the value of the hand. 
  // Ace is counted as 10 unless this will cause the value to exceed 21
	public int value() {
    int handValue = 0;
    int cardValue = 0;
    for(int i = 0 ; i< hand.size() ; i++) {
      cardValue = ((Card)(hand.elementAt(i))).getValue();
      handValue += cardValue >= 10 ? 10 : cardValue;
    }
    if(hasAce() && handValue < 12)
      handValue += 10;
      
    return handValue;

	}

  // Tests for blackjack
  public boolean isBlackjack() {
    return (hand.size() == 2 && hasAce() && hasTen());
  }

  // Calculates whether the value of a hand exceeds 21
  public boolean isBust() {
    return value() > 21;
  }

  // Tests for an Ace in the hand
  public boolean hasAce() {
    for(int i = 0 ; i< hand.size() ; i++)
      if(((Card)(hand.elementAt(i))).getValue()==Card.ACE)
        return true;

    return false;
  }

  // Tests for a 10, J, Q, or K in the hand
  public boolean hasTen() {
    for(int i = 0 ; i< hand.size() ; i++)
      if(((Card)(hand.elementAt(i))).getValue() >= 10)
        return true;

    return false;
  }
  

  public String toString() {
    StringBuffer str = new StringBuffer();
    for(int i = 0 ; i< hand.size() ; i++)
      str.append(((Card)(hand.elementAt(i))).toString()).append(' ');
    return str.toString();

  }

	private Vector hand = new Vector();			// Stores the cards in the hand
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -