我的程序没有在另一个类中找到已定义的方法

时间:2011-06-11 03:10:25

标签: java

我的卡类,其中定义了方法。

package blackjackgamemodel;

public class Card {
    protected Rank rank;
    protected Suit suit;

    public Card(Card.Rank rank, Card.Suit suit) {
        this.rank = rank;
        this.suit = suit;
    }

    public String toString() {
        return "" + rank + " of " + suit;
    }

    public Card.Rank rank() {
        return rank;
    }

    public Card.Suit suit() {
        return suit;
    }

    public enum Rank {
        ACE (1), TWO (2), THREE (3), FOUR (4), FIVE (5), SIX (6), SEVEN (7),
        EIGHT (8), NINE (9), TEN (10), JACK (11), QUEEN (12), KING (13);

        private int value;

        Rank(int value) {
            this.value = value;
        }

        public int value() {
            return value;
        }
    }

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

}

错误所在的游戏类

package blackjackgamemodel;

import blackjackgamemodel.Card;
import blackjackgamemodel.Deck;

public class Game {
    protected Deck deck;
    protected int sum;
    protected int aces;


    public Game() {
        // Initialize deck, sum and aces.
        deck = new Deck();
        sum = 0;
        aces = 0;
    }

    public Card draw() {

        // Draw a card from the deck
        Card drawn_card = deck.draw();

        // Calculate the value to add to sum
        int v = drawn_card.value();
        if (v == 1) {
            v = 11;
            // If the card is an ace, increase the count of aces.
            aces++;
        }
        else if (v > 10) {
            v = 10;
        }
        // Now v is the Blackjack value of the card.

        // If the sum is greater than 21 and there are aces,
        //   Then decrease the sum by 10 and the aces by 1.
        if (aces > 0 && sum > 21) {
            sum = sum - 10;
            aces = aces - 1;
        }

        // Return the card that was drawn.
    return drawn_card; 

    }

    public int sum() {
        // Getter for sum.
        return sum;

    }
}

ERROR

Description Resource    Path    Location    Type
The method value() is undefined for the type Card   Game.java   /BlackJack/src/blackjackgamemodel   line 25 Java Problem

5 个答案:

答案 0 :(得分:3)

具有Card.Rank方法的value()枚举,而不是Card类。

尝试:

int v = drawn_card.rank().value()

答案 1 :(得分:2)

确实Card没有value方法:它有rank方法,Card.Rankvalue方法。

答案 2 :(得分:1)

value()不是Card中包含代码的Card.Rank上的方法。

答案 3 :(得分:1)

消息正确,Card没有value()方法。您的意思是获得卡的排名,然后获得该值?这看起来像是意图。

答案 4 :(得分:0)

您的卡有价值方法吗?看起来卡的排名有一个价值方法。

尝试:

int v = drawn_card.rank().value();