卡类中找不到符号

时间:2018-12-02 06:09:45

标签: java

在这里,我创建了一个Card类别,该类别赋予了牌值和排名,我还创建了一个Deck类别,该类别提供了52张牌,标准扑克牌之一,随机播放方法,deal方法,report方法(卡座中剩余的数字)和toString方法。当我尝试制作新的纸牌时,我的问题出在Deck班上。我从Deck类收到此错误:

Deck.java:12: error: cannot find symbol
      for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++)               
symbol:   variable DIAMONDS
location: class Card

Deck.java:12: error: cannot find symbol
      for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++)

symbol:   variable SPADES

我尝试将Card.DIAMONDS更改为Suit.DIAMONDS,这给了我另一个错误。任何关于我的问题所在方法的帮助将不胜感激。对我来说,这是一次学习经历,所以我喜欢指导而不是确切的解决方案。

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

enum Rank {
    ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING
};

public class Card {

private Rank aRank;
private Suit aSuit;

public Card(Suit aSuit, Rank aRank) {
    this.aSuit = aSuit;
    this.aRank = aRank;
}

public Rank getRank() {
    return aRank;
}

public Suit getSuit() {
    return aSuit;
}

public String toString() {
    String cardValue = "";
    String rank = "";
    String suit = "";
    switch (aSuit) {
    case HEARTS:
      suit = "hearts";
      break;
    case DIAMONDS:
      suit = "diamonds";
      break;
    case CLUBS:
      suit = "clubs";
      break;
    case SPADES:
      suit = "spades";
      break;
  }

switch (aRank) {
  case ACE:
    rank = "Ace";
    break;
  case TWO:
    rank = "2";
    break;
  case THREE:
    rank = "3";
    break;
  case FOUR:
    rank = "4";
    break;
  case FIVE:
    rank = "5";
    break;
  case SIX:
    rank = "6";
    break;
  case SEVEN:
    rank = "7";
    break;
  case EIGHT:
    rank = "8";
    break;
  case NINE:
    rank = "9";
    break;
  case TEN:
    rank = "10";
    break;
  case JACK:
    rank = "Jack";
    break;
  case QUEEN:
    rank = "Queen";
    break;
  case KING:
    rank = "King";
    break;
}
cardValue += rank + " of " + suit;
return cardValue;
}

public int compareTo(Card other) {
    int rankComparison = aRank.compareTo(other.aRank);
    return rankComparison != 0 ? rankComparison : aSuit.compareTo(other.aSuit);
}

public boolean equals(Card other) {
    if (aRank == other.aRank)
        return true;
    if (aSuit == other.aSuit)
        return true;
    return false;
}

// Main method to test.
public static void main(String[] args) {
    Card c1 = new Card(Suit.SPADES, Rank.FIVE);
    Card c2 = new Card(Suit.HEARTS, Rank.TWO);
    Card c3 = new Card(Suit.CLUBS, Rank.EIGHT);
    Card c4 = new Card(Suit.DIAMONDS, Rank.FIVE);

    Card r1 = new Card(Suit.CLUBS, Rank.ACE);
    Card r2 = new Card(Suit.DIAMONDS, Rank.JACK);
    Card r3 = new Card(Suit.HEARTS, Rank.QUEEN);
    Card r4 = new Card(Suit.SPADES, Rank.KING);

    System.out.println(c1);
    System.out.println(c2);
    System.out.println(c3);

    if (c1.compareTo(c2) < 0)
        System.out.println(c2 + " outranks " + c1);
    else if (c1.compareTo(c2) > 0)
        System.out.println(c1 + " outranks " + c2);

    if (c1.compareTo(c3) < 0)
        System.out.println(c3 + " outranks " + c1);
    else if (c1.compareTo(c3) > 0)
        System.out.println(c1 + " outranks " + c3);

    if (c1.compareTo(c1) == 0)
        System.out.println(c1 + " is equal to " + c1);
    else
        System.out.println(c1 + " is NOT equal to " + c1);

    if (c1.equals(c4))
        System.out.println(c1 + " is equal to " + c4);
    else
        System.out.println(c1 + " is NOT equal to " + c4);

    if (r1.compareTo(r2) < 0)
        System.out.println(r1 + " comes before " + r2);
    else if (r1.compareTo(r2) > 0)
        System.out.println(r1 + " comes after " + r2);
    else
        System.out.println(r1 + " is equal to " + r2);

    if (r4.compareTo(r3) < 0)
        System.out.println(r4 + " comes before " + r3);
    else if (r4.compareTo(r3) > 0)
        System.out.println(r4 + " comes after " + r3);
    else
        System.out.println(r4 + " is equal to " + r3);
}
}

我的甲板课:

import java.lang.reflect.Array;
import java.util.*;
public class Deck{
   public static final int NEWCARDS = 52;
   private Card[] deckOfCards;        
   private int currentCard;           

   public void Deck( ) {
      deckOfCards = new Card[ NEWCARDS ];
      int i = 0;

      for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++)
         for ( int rank = 1; rank <= 13; rank++ )
             deckOfCards[i++] = new Card(suit, rank);
             currentCard = 0;
   }
   public void shuffle(int n) {
      int i, j, k;

      for ( k = 0; k < n; k++ ){ 
      i = (int) ( NEWCARDS * Math.random() );  
      j = (int) ( NEWCARDS * Math.random() );  

      Card temp = deckOfCards[i];
      deckOfCards[i] = deckOfCards[j];
      deckOfCards[j] = temp;
      }
      currentCard = 0;   
   }

   public Card deal() {

      if (currentCard < NEWCARDS) {
         return ( deckOfCards[currentCard++] );
      }
      else{
         System.out.println("Out of cards error");
         return ( null );  
      }
   }

   public String toString() {
      String s = "";
      int k;
      k = 0;

      for ( int i = 0; i < 4; i++ ) {        
          for ( int j = 1; j <= 13; j++ )
             s += (deckOfCards[k++] + " ");
             s += "\n";
     }
      return (s);
   }
}

1 个答案:

答案 0 :(得分:2)

enum不属于该类。它也不是int。更改

for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++)

要访问Suit,则需要ordinal()才能分配给int。像

for (int suit = Suit.DIAMONDS.ordinal(); suit <= Suit.SPADES.ordinal(); suit++) {
    Suit s = Suit.values()[suit];
    // ...
}