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

📄 card.java

📁 Java Classic Examples是我买的两本书:《JAVA经典实例》和《java入门经典源代码》里边附送光盘里带的源码
💻 JAVA
字号:
class Card implements Comparable
{

  public Card(int value, int suit) throws IllegalArgumentException
  {
    if(value >= ACE && value <= KING)
      this.value = value;
    else
      throw new IllegalArgumentException("Invalid card value");
    if(suit >= HEARTS && suit <= SPADES)
      this.suit = suit;
    else
      throw new IllegalArgumentException("Invalid suit");
  }

  // Compare two cards
  public int compareTo(Object card)
  {
    if(suit != ((Card)card).suit)
      return suit < ((Card)card).suit ? -1: 1;
    else
      if(value == ((Card)card).value)
        return 0;
      else
        return value < ((Card)card).value ? -1 : 1;
  }


  public String toString()
  {
    String cardStr;
    switch(value)
    {
      case ACE: cardStr = "A";
                break;
      case JACK: cardStr = "J";
                break;
      case QUEEN: cardStr = "Q";
                break;
      case KING: cardStr = "K";
                break;
      default: cardStr = Integer.toString(value);
                break;
    }

    switch(suit)
    {
      case CLUBS: cardStr += "C";
                break;
      case DIAMONDS: cardStr += "D";
                break;
      case HEARTS: cardStr += "H";
                break;
      case SPADES: cardStr += "S";
                break;
    }
    return cardStr;
  }






  // Suit values
  public static final int HEARTS = 0;
  public static final int CLUBS = 1;
  public static final int DIAMONDS = 2;
  public static final int SPADES = 3;

  // Card face values
  public static final int ACE = 1;
  public static final int JACK = 11;
  public static final int KING = 12;
  public static final int QUEEN = 13;

  private int suit;
  private int value;
}

⌨️ 快捷键说明

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