为什么它总是告诉我“变量卡可能没有被初始化”
问题描述:
这是一个java扑克游戏项目。一开始,我定义了卡类。为什么它总是告诉我“变量卡可能没有被初始化”
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类来持有卡。但是我在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;
/**
* Constructor: Creates default one deck of 52 playing cards in originalDecks and
* copy them to dealDecks.
* initialize numberDecks=n
* Note: You need to catch MyPlayingCardException from Card constructor
* Use ArrayList for both originalDecks & dealDecks
*/
public Decks()
{
// implement this method!
ArrayList<Card> originalDecks = new ArrayList<Card>(52);
ArrayList<Card> dealDecks = new ArrayList<Card>(52);
Card card;
for (int i=1; i<=3; i++) {
for (int j=1; j<= 13; j++) {
try{
card = new Card(j,i);
}catch (MyPlayingCardException e){
System.out.println("MyPlayingCardException: "+e.getMessage());
}
originalDecks.add(card);
}
}
dealDecks.addAll(originalDecks);
}
/**
* Constructor: Creates n decks (52 cards each deck) of playing cards in
* originalDecks and copy them to dealDecks.
* initialize numberDecks=n
* Note: You need to catch MyPlayingCardException from Card constructor
* Use ArrayList for both originalDecks & dealDecks
*/
public Decks(int n)
{
int numberDecks=n ;
Card card;
for (int m=0; m<n; m++){
for (int i=0; i<=3; i++) {
for (int j=0; j<= 13; j++) {
try{
card = new Card(j,i);
}
catch (MyPlayingCardException e){
System.out.println("MyPlayingCardException: "+e.getMessage());
}
originalDecks.add(card);
}
dealDecks.addAll(originalDecks);
}
}
}
答
编译器抱怨这条线......
originalDecks.add(card);
...它是正确的。
如果Card
构造函数抛出一个MyPlayingCardException
那么当你第一次尝试初始化card
变量,然后控制就会传递到catch
块没有card
已经初始化。 catch
块会打印一条消息,但只允许控制落入该问题的线路。您可以捕获异常并输出消息,但在这种情况下,catch块也必须抛出一个异常(无论是相同还是不同的异常),否则它必须以某种方式初始化card
本身。
请注意,如果Card
构造函数在内循环的后续迭代中抛出MyPlayingCardException
,则card
将保留其先前的值。虽然在这种情况下它不会未初始化,但它所具有的价值并不是您实际想要使用的价值。
将originalDecks.add(卡)移至try块。这解决了问题。 – 2014-12-05 20:53:53