这是我学校项目中的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 numberDecks=n ;
Decks originalDecks = new Decks();
for (int m=0; m< n; m++){
originalDecks += originalDecks ;
}
}
这个想法是:首先,我尝试用52张卡创建一个牌组;然后我绑定它循环n次以创建n个套牌。但是,我得到了一些未解决的错误,告诉我
找不到符号i,j。
为什么?
答案 0 :(得分:3)
因为您在定义Card
和i
之前尝试创建j
。另外,请使用牙套。
// Card card = new Card(j,i);
for (int i=0; i<=3; i++) {
for (int j=0; j<= 13; j++) {
Card card = new Card(j,i);
originalDecks.add(card);
}
}
答案 1 :(得分:2)
此声明
Card card = new Card(j,i);
范围内没有变量i
或j
,因此它是合法编译错误
你想在内环中移动它们
for (int i=0; i<=3; i++) {
for (int j=0; j<= 13; j++) {
Card card = new Card(j,i);
originalDecks.add(card);
}
}
答案 2 :(得分:1)
主要是RuntimeError“此处不允许变量声明”是因为缺少大括号。
实施例: -
public class Creator
{
public static void main(String[] args)
{
for(int i=0;i<100;i++)
Creature creature=new Creature(); // Error
System.out.println(Creature.numCreated());
}
}
编译此代码时出错:此处不允许变量声明。
但是,
for(int i=0;i<100;i++)
{Creature creature=new Creature();} //no error
这将解决错误。