Reputation: 75
This is a java poker project from my school project.
At the beginning, Card class is defined.
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]; }
Then, I tried to define the Deck class. But I had some error.
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);
}
public Decks(int n) {
int numberDecks=n ;
Decks originalDecks = new Decks();
for (int m=0; m< n; m++){
originalDecks += originalDecks ;
}
}
The idea is that: first, I tried to create one deck with 52 cards; then I tied to loop it n times to create n decks. However, I got some unresolved error showing me that
Cannot find symbol i, j.
Why is that?
Upvotes: 1
Views: 2418
Reputation: 661
Mostly RuntimeError "variable declaration is not allowed here" is because of lack of Braces.
Example:-
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());
}
}
On compiling this code is giving error: variable declaration is not allowed here.
But,
for(int i=0;i<100;i++)
{Creature creature=new Creature();} //no error
This will resolve the error.
Upvotes: 1
Reputation: 201409
Because you try to create a Card
before you define i
and j
. Also, please use braces.
// 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);
}
}
Upvotes: 3
Reputation: 240860
for this statement
Card card = new Card(j,i);
there is no variable i
or j
in the scope and so it is legit compilation error
you want to move them inside inner loop
for (int i=0; i<=3; i++) {
for (int j=0; j<= 13; j++) {
Card card = new Card(j,i);
originalDecks.add(card);
}
}
Upvotes: 2