Java错误:无法找到符号

时间:2015-05-27 12:55:07

标签: java blackjack

这个小部分仍然是一个问题。

if (card.suit == Suit.DIAMOND || card.suit == Suit.HEART) {
             g.setColor(Color.red);
         } else {
            g.setColor(Color.black);
}

我得到的错误是“找不到符号”,符号为“适合”。

当我把它更改为。

if (card.suit == PlayingCard.DIAMOND || card.suit == PlayingCard.HEART) {
             g.setColor(Color.red);
         } else {
            g.setColor(Color.black);
}

我仍然收到错误“找不到符号”,符号为“ .DIAMOND ”和“ .HEART

我的 PlayingCard

public class PlayingCard
 {
  // Instance Data - all things common to all cards
  private String cardFace; // king, q, j, 10 - 2, A
  private int faceValue; // numberic value of the card
  private char cardSuit; // hold suit of the card
private char suits[] = {(char)(003), (char)(004), (char)(005), (char)(006)};
 Suit suit;
Rank rank;
// Constructor
public PlayingCard(int value, int suit)
{
    faceValue = value;
    setFace();
    setSuit(suit);
}

// helper setFace()
public void setFace()
{
    switch(faceValue)
    {
        case 1:
            cardFace = "A";
            faceValue = 14;
            break;
        case 11:
            cardFace = "J";
            break;
        case 12:
            cardFace = "Q";
            break;
        case 0:
            cardFace = "K";
            faceValue = 13;
            break;
        default:
            cardFace = ("" + faceValue);
    }
}

public void setSuit(int suit) // suit num between 0 and 3
{
    cardSuit = suits[suit];
}

// other helpers
public int getFaceValue()
{
    return faceValue;
}
public String getCardFace()
{
    return cardFace;
}

public String toString()
{
    return (cardFace + cardSuit);
}
   public static enum Suit {
        CLUB, DIAMOND, SPADE, HEART
    }

    public static enum Rank {
        TWO("2"), THREE("3"), FOUR("4"), FIVE("5"), SIX("6"), SEVEN("7"),
        EIGHT("8"), NINE("9"), TEN("10"), JACK("J"), QUEEN("Q"), KING("K"),
        ACE("A");

        private final String symbol;

        Rank(String symbol) {
            this.symbol = symbol;
        }

        public String getSymbol() {
            return symbol;
        }
    }

}

这也是我所说的有问题的完整课程。 哪个是我的 GUI 类。

public class BlackjackGUI extends Applet {

   public void init() {

         // The init() method lays out the applet using a BorderLayout.
         // A BlackjackCanvas occupies the CENTER position of the layout.
         // On the bottom is a panel that holds three buttons.  The
         // HighLowCanvas object listens for ActionEvents from the buttons
         // and does all the real work of the program.

      setBackground( new Color(130,50,40) );
      setLayout( new BorderLayout(3,3) );

      BlackjackCanvas board = new BlackjackCanvas();
      add(board, BorderLayout.CENTER);

      Panel buttonPanel = new Panel();
      buttonPanel.setBackground( new Color(220,200,180) );
      add(buttonPanel, BorderLayout.SOUTH);

      Button hit = new Button( "Hit!" );
      hit.addActionListener(board);
      hit.setBackground(Color.lightGray);
      buttonPanel.add(hit);

      Button stay = new Button( "Stay!" );
      stay.addActionListener(board);
      stay.setBackground(Color.lightGray);
      buttonPanel.add(stay);

      Button newGame = new Button( "New Game" );
      newGame.addActionListener(board);
      newGame.setBackground(Color.lightGray);
      buttonPanel.add(newGame);

   }  // end init()

   public Insets getInsets() {
         // Specify how much space to leave between the edges of
         // the applet and the components it contains.  The background
         // color shows through in this border.
      return new Insets(3,3,3,3);
   }

} // end class HighLowGUI


class BlackjackCanvas extends Canvas implements ActionListener {

      // A class that displays the card game and does all the work
      // of keeping track of the state and responding to user events.

  DeckOfCards BJDeck = new DeckOfCards();        // A deck of cards to be used in the game.

   BlackjackHand dealerHand;   // Hand containing the dealer's cards.
   BlackjackHand playerHand;   // Hand containing the user's cards.

   String message;  // A message drawn on the canvas, which changes
                    //    to reflect the state of the game.

   boolean gameInProgress;  // Set to true when a game begins and to false
                            //   when the game ends.

   Font bigFont;      // Font that will be used to display the message.
   Font smallFont;    // Font that will be used to draw the cards.


   BlackjackCanvas() {
         // Constructor.  Creates fonts and starts the first game.
      setBackground( new Color(0,120,0) );
      smallFont = new Font("SansSerif", Font.PLAIN, 12);
      bigFont = new Font("Serif", Font.BOLD, 14);
      doNewGame();
   }


   public void actionPerformed(ActionEvent evt) {
          // Respond when the user clicks on a button by calling
          // the appropriate procedure.  Note that the canvas is
          // registered as a listener in the BlackjackGUI class.
      String command = evt.getActionCommand();
      if (command.equals("Hit!"))
         doHit();
      else if (command.equals("Stand!"))
         doStand();
      else if (command.equals("New Game"))
         doNewGame();
   }


   void doHit() {
          // This method is called when the user clicks the "Hit!" button.
          // First check that a game is actually in progress.  If not, give
          // an error message and exit.  Otherwise, give the user a card.
          // The game can end at this point if the user goes over 21 or
          // if the user has taken 5 cards without going over 21.
      if (gameInProgress == false) {
         message = "Click \"New Game\" to start a new game.";
         repaint();
         return;
      }
      playerHand.addCard( BJDeck.getTopCard() );
      if ( playerHand.getBlackjackValue() > 21 ) {
         message = "You've busted!  Sorry, you lose.";
         gameInProgress = false;
      }
      else if (playerHand.getCardCount() == 5) {
         message = "You win by taking 5 cards without going over 21.";
         gameInProgress = false;
      }
      else {
         message = "You have " + playerHand.getBlackjackValue() + ".  Hit or Stand?";
      }
      repaint();
   }


   void doStand() {
           // This method is called when the user clicks the "Stand!" button.
           // Check whether a game is actually in progress.  If it is,
           // the game ends.  The dealer takes cards until either the
           // dealer has 5 cards or more than 16 points.  Then the
           // winner of the game is determined.
      if (gameInProgress == false) {
         message = "Click \"New Game\" to start a new game.";
         repaint();
         return;
      }
      gameInProgress = false;
      while (dealerHand.getBlackjackValue() <= 16 && dealerHand.getCardCount() < 5)
         dealerHand.addCard( BJDeck.getTopCard() );
      if (dealerHand.getBlackjackValue() > 21)
          message = "You win!  Dealer has busted with " + dealerHand.getBlackjackValue() + ".";
      else if (dealerHand.getCardCount() == 5)
          message = "Sorry, you lose.  Dealer took 5 cards without going over 21.";
      else if (dealerHand.getBlackjackValue() > playerHand.getBlackjackValue())
          message = "Sorry, you lose, " + dealerHand.getBlackjackValue()
                                            + " to " + playerHand.getBlackjackValue() + ".";
      else if (dealerHand.getBlackjackValue() == playerHand.getBlackjackValue())
          message = "Sorry, you lose.  Dealer wins on a tie.";
      else
          message = "You win, " + playerHand.getBlackjackValue()
                                            + " to " + dealerHand.getBlackjackValue() + "!";
      repaint();
   }


   void doNewGame() {
          // Called by the constructor, and called by actionPerformed() if
          // the use clicks the "New Game" button.  Start a new game.
          // Deal two cards to each player.  The game might end right then
          // if one of the players had blackjack.  Otherwise, gameInProgress
          // is set to true and the game begins.
      if (gameInProgress) {
              // If the current game is not over, it is an error to try
              // to start a new game.
         message = "You still have to finish this game!";
         repaint();
         return;
      }
      DeckOfCards BJDeck = new DeckOfCards();   // Create the deck and hands to use for this game.
      dealerHand = new BlackjackHand();
      playerHand = new BlackjackHand();
      BJDeck.shuffleDeck();
      dealerHand.addCard( BJDeck.getTopCard() ); // Deal two cards to each player.
      dealerHand.addCard( BJDeck.getTopCard() );
      playerHand.addCard( BJDeck.getTopCard() );
      playerHand.addCard( BJDeck.getTopCard() );
      if (dealerHand.getBlackjackValue() == 21) {
          message = "Sorry, you lose.  Dealer has Blackjack.";
          gameInProgress = false;
      }
      else if (playerHand.getBlackjackValue() == 21) {
          message = "You win!  You have Blackjack.";
          gameInProgress = false;
      }
      else {
          message = "You have " + playerHand.getBlackjackValue() + ".  Hit or stay?";
          gameInProgress = true;
      }
      repaint();
   }  // end newGame();


   public void paint(Graphics g) {
         // The paint method shows the message at the bottom of the
         // canvas, and it draws all of the dealt cards spread out
         // across the canvas.

      g.setFont(bigFont);
      g.setColor(Color.green);
      g.drawString(message, 10, getSize().height - 10);

      // Draw labels for the two sets of cards.

      g.drawString("Dealer's Cards:", 10, 23);
      g.drawString("Your Cards:", 10, 153);

      // Draw dealer's cards.  Draw first card face down if
      // the game is still in progress,  It will be revealed
      // when the game ends.

       g.setFont(smallFont);
                if (gameInProgress)
                   drawCard(g, null, 10, 30);
                else
                   drawCard(g, dealerHand.getCard(0), 10, 30);
                for (int i = 1; i < dealerHand.getCardCount(); i++)
         drawCard(g, dealerHand.getCard(i), 10 + i * 90, 30);

      // Draw the user's cards.

      for (int i = 0; i < playerHand.getCardCount(); i++)
         drawCard(g, playerHand.getCard(i), 10 + i * 90, 160);

   }  // end paint();


   void drawCard(Graphics g, PlayingCard card, int x, int y) {
           // Draws a card as a 80 by 100 rectangle with
           // upper left corner at (x,y).  The card is drawn
           // in the graphics context g.  If card is null, then
           // a face-down card is drawn.  (The cards are
           // rather primitive.)

      if (card == null) {
             // Draw a face-down card
         g.setColor(Color.blue);
         g.fillRect(x,y,80,100);
         g.setColor(Color.white);
         g.drawRect(x+3,y+3,73,93);
         g.drawRect(x+4,y+4,71,91);
      }
      else {
         g.setColor(Color.white);
         g.fillRect(x,y,80,100);
         g.setColor(Color.gray);
         g.drawRect(x,y,79,99);
         g.drawRect(x+1,y+1,77,97);
         if (card.suit == PlayingCard.DIAMOND || card.suit == PlayingCard.HEART) {
             g.setColor(Color.red);
         } else {
            g.setColor(Color.black);
}

      }
   }  // end drawCard()


} // end class BlackjackCanvas

2 个答案:

答案 0 :(得分:0)

您遇到此问题的原因是您已将Suit声明为PlayingCard的静态嵌套类,并且您没有正确引用它。

为了解决这个问题,你应该将Suit移到它自己的班级并删除static修饰符。

你也可以通过引用这样的套装来解决这个问题:

PlayingCard.Suit.DIAMOND

但第一个解决方案更好,因为Suit似乎是PlayingCard的一个独特实体,所以它应该在它自己的类中。

答案 1 :(得分:0)

你离我不远:)

if (card.suit == Suit.DIAMOND || card.suit == Suit.HEART) {
    g.setColor(Color.red);
} else {
    g.setColor(Color.black);
}

这个是正确的(事实上,你也可以写PlayingCard.Suit.HEART,具体取决于你的import,而不是PlayingCard.HEART),但是card.suit存在可见性问题}。
您未在private字段中提供publicprotectedsuit,因此只能在其包中访问。

我想这段代码在另一个包中,所以你必须使用getter(推荐),或者将suit设置为public