【发布时间】:2014-12-04 18:45:05
【问题描述】:
这是我学校项目中的一个 Java 扑克项目。
一开始就定义了 Card 类。
class Card {
/* constant suits and ranks */
static final String[] Suit = {"Clubs", "Diamonds", "Hearts", "Spades" };
static final String[] Rank = {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};
/* Data field of a card: rank and suit */
private int cardRank; /* values: 1-13 (see Rank[] above) */
private int cardSuit; /* values: 0-3 (see Suit[] above) */
/* Constructor to create a card */
/* throw MyPlayingCardException if rank or suit is invalid */
public Card(int rank, int suit) throws MyPlayingCardException {
if ((rank < 1) || (rank > 13))
throw new MyPlayingCardException("Invalid rank:"+rank);
else
cardRank = rank;
if ((suit < 0) || (suit > 3))
throw new MyPlayingCardException("Invalid suit:"+suit);
else
cardSuit = suit;
}
/* Accessor and toString */
/* You may impelemnt equals(), but it will not be used */
public int getRank() { return cardRank; }
public int getSuit() { return cardSuit; }
public String toString() { return Rank[cardRank] + " " + Suit[cardSuit]; }
然后,我尝试定义 Deck 类。但我有一些错误。
class Decks {
/* this is used to keep track of original n*52 cards */
private List<Card> originalDecks;
/* this starts with n*52 cards deck from original deck */
/* it is used to keep track of remaining cards to deal */
/* see reset(): it resets dealDecks to a full deck */
private List<Card> dealDecks;
/* number of decks in this object */
private int numberDecks;
public Decks()
{
ArrayList<Card> originalDecks = new ArrayList<Card>(52);
ArrayList<Card> dealDecks = new ArrayList<Card>(52);
Card card = new Card(j,i); //Error
for (int i=0; i<=3; i++)
for (int j=0; j<= 13; j++)
originalDecks.add(card); //Error
dealDecks.addAll(originalDecks);
}
公共甲板(int n) {
int numberDecks=n ;
Decks originalDecks = new Decks();
for (int m=0; m< n; m++){
originalDecks += originalDecks ;
}
}
这个想法是:首先,我尝试用 52 张牌创建一副牌;然后我将它捆绑循环 n 次以创建 n 套牌。但是,我遇到了一些未解决的错误,告诉我
找不到符号 i、j。
这是为什么呢?
【问题讨论】:
标签: java constructor