这是我的学校项目中的java扑克项目。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个套牌。但是,我得到了一些未解决的错误告诉我,
Cannot find symbol i, j.
这是为什么?
我试图将其移入,但它显示新的错误:“变量声明不允许在这里”。 – 2014-12-04 18:52:09
@李杰洲你有没有加上大括号? – 2014-12-04 18:52:42
解决了这个问题。非常感谢您的快速回复! – 2014-12-04 18:55:51