Java: Cannot find symbol?

2019-03-06 17:07发布

问题:

When I try to compile a small program I wrote that builds an array of a deck of cards and prints them, I get 6 errors all of which are "cannot find symbol" errors. Can somebody check my code and find the mistakes please? Thanks.

Compile Output:

sudhakar@sudhakar-Dell-DXP061 ~/deck $ javac doDeck.java Deck.java Card.java
doDeck.java:7: cannot find symbol
symbol  : variable deck
location: class carddeck.Deck
        System.out.println(mydeck.deck[x].getRankString() + " of " + mydeck.deck[x].getSuitString());
                                 ^
doDeck.java:7: cannot find symbol
symbol  : variable deck
location: class carddeck.Deck
        System.out.println(mydeck.deck[x].getRankString() + " of " + mydeck.deck[x].getSuitString());
                                                                           ^
Deck.java:4: cannot find symbol
symbol  : variable deck
location: class carddeck.Deck
    deck = new Card[51];
    ^
Deck.java:10: cannot find symbol
symbol  : variable deck
location: class carddeck.Deck
            this.deck[cardcount] = new Card(suitit,rankit);
                ^
Deck.java:10: cannot find symbol
symbol  : variable cardcount
location: class carddeck.Deck
            this.deck[cardcount] = new Card(suitit,rankit);
                      ^
Deck.java:11: cannot find symbol
 symbol  : variable cardcount
location: class carddeck.Deck
                    cardcount++;
                    ^
6 errors

Deck.java:

package carddeck;
class Deck {
    public Deck () {
        deck = new Card[51];
        int cardcount = 0;
    }
     public void buildDeck () {
        for (Card.Rank rankit: Card.Rank.values()) {
            for (Card.Suit suitit: Card.Suit.values()) {
                this.deck[cardcount] = new Card(suitit,rankit);
                cardcount++;
            };
        };
    };
}

回答1:

You ave no property deck in your class, try adding it like that:

class Deck {
    private Card[] deck = null;
    public Deck () {
        deck = new Card[51];
        int cardcount = 0;
    }
//...
}


回答2:

How are you accessing deck, it's not defined.

class Deck {

Card deck;
int cardcount;
public Deck () {

    deck = new Card[51];
    cardcount = 0;
}

.....