-1

我在编译这个 DeckOfCards 类时遇到问题。Deck2.Shuffle()它一直告诉我指向and时找不到符号Deck2.deal()。任何想法为什么?

import java.lang.Math;
import java.util.Random;

public class DeckOfCards {
    private Cards[] Deck;
    private int cardHold;

    public DeckOfCards() {
        Deck = new Cards[52];
        int n = 0;
        for (int i = 1; i <= 13; i++) {
            for (int j = 1; j <= 4; j++) {
                Deck[n] = new Cards(i, j);
                n = n + 1;
            }
        }
        cardHold = -1;
    }

    public void Shuffle() {
        // shuffles ands resets deck
        int i = 0;
        while (i < 52) {
            int rando = (int) (5.0 * (Math.random()));
            Cards temp = Deck[rando];
            Deck[rando] = Deck[i];
            Deck[i] = temp;
            i++;
        }
    }

    public Cards deal() {
        // if there are any more cards left in the deck, return the next one and
        // increment
        // index; return null if all the cards have been dealt
        // ***Question, increment before or
        // after??***----------------------------------------
        if (!hasMoreCards()) {
            return null;
        } else {
            Cards temp = null;
            temp = Deck[cardHold];
            cardHold = cardHold + 1;
            return temp;
        }
    }

    public boolean hasMoreCards() {
        // returns true if there are more cards left, else return false
        if (cardHold == 0)
            return false;
        else
            return true;
    }

    public static void main(String[] args) {
        DeckOfCards Deck2 = new DeckOfCards();
        Deck2.Shuffle();
        for (int i = 0; i < 52; i++)
            System.out.println(Deck2.deal());
    }
}

下面的类是 Card 类,这可能是导致问题的原因吗?

public class Cards {
    protected int rank;
    protected int suit;
    protected String[] sNames = { "Hearts", "Clubs", "Spades", "Diamonds" };
    protected String[] rNames = { "Ace", "2", "3", "4", "5", "6", "7", "8",
            "9", "10", "Jack", "Queen", "King" };

    public Cards(int Rank, int Suit) {
        suit = Suit;
        rank = Rank;
    }

    public String toString() {
        return ("Your card is: " + rNames[rank - 1] + " of " + sNames[suit - 1]);
    }

    public int getRank() {
        return rank;
    }

    public int getSuit() {
        return suit;
    }

}
4

4 回答 4

1

您在if此处的语句表达式中使用了赋值运算符:

if (cardHold = 0)

用。。。来代替

if (cardHold == 0)
于 2013-04-05T02:33:47.647 回答
1

你的编译器问题已经过去了,但是你的代码有很多问题。

public class DeckOfCards {
    private Cards[] deck;
    private int cardHold;

    public DeckOfCards() {
        deck = new Cards[52];
        int n = 0;
        for (int i = 1; i <= 13; i++) {
            for (int j = 1; j <= 4; j++) {
                deck[n] = new Cards(i, j);
                n = n+1;
            }
        }
        cardHold = -1;
    }

    public void shuffle() {
        int i = 0;
        while (i < 52) {
            int rando = (int) (5.0*(Math.random()));
            Cards temp = deck[rando];
            deck[rando] = deck[i];
            deck[i] = temp;
            i++;
        }
    }

    public Cards deal() {
        return (hasMoreCards() ? deck[++cardHold] : null);
    }

    public boolean hasMoreCards() {
        return (cardHold != 0);
    }

    public static void main(String[] args) {
        DeckOfCards deck2 = new DeckOfCards();
        deck2.shuffle();
        for (int i = 0; i < 52; i++)
            System.out.println(deck2.deal());
    }
}

和:

public class Cards {
    protected int rank;
    protected int suit;
    protected String[] sNames = {"Hearts", "Clubs", "Spades", "Diamonds"};
    protected String[] rNames = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

    public Cards(int rank, int suit) {
        this.suit = suit;
        this.rank = rank;
    }

    public String toString() {
        return ("Your card is: "+rNames[rank-1]+" of "+sNames[suit-1]);
    }

    public int getRank() {
        return rank;
    }

    public int getSuit() {
        return suit;
    }
}
于 2013-04-05T02:36:32.017 回答
0

您可能想要添加 import java.util.Date; 并且主程序中的随机播放功能和调用不同,一个有's',另一个有'S'。

于 2013-04-05T02:53:55.773 回答
0

试试这个:

import java.lang.Math;
import java.util.Date;
import java.util.Random;

public class DeckOfCards {

    private Cards[] Deck;
    private int cardHold;
    private Random rand;

    public DeckOfCards() {
        rand = new Random();
        Date d = new Date();
        rand.setSeed(d.getTime());
        Deck = new Cards[52];
        int n = 0;
        for (int i = 1; i <= 13; i++) {
            for (int j = 1; j <= 4; j++) {
                Deck[n] = new Cards(i, j);
                n = n + 1;
            }
        }
        cardHold = 0;
    }

    public void shuffle() {
        // shuffles ands resets deck
        int i = 0;
        while (i < 52) {
            // int rando = (int)(5.0 * (Math.random()));
            int rando = rand.nextInt(52);
            Cards temp = Deck[rando];
            Deck[rando] = Deck[i];
            Deck[i] = temp;
            i++;
        }
    }

    public boolean hasMoreCards() {
        // returns true if there are more cards left, else return false
        if (cardHold == 0)
            return false;
        else
            return true;
    }

    public static void main(String[] args) {
        DeckOfCards Deck2 = new DeckOfCards();
        Deck2.shuffle();
        for (int i = 0; i < 52; i++)
            System.out.println(Deck2.Deck[i]);

    }
}
于 2013-04-05T03:37:56.800 回答