2014-12-05 44 views
0

這是一個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); 
     } 
    }    

} 

回答

0

編譯器抱怨這條線......

  originalDecks.add(card); 

...它是正確的。

如果Card構造函數拋出一個MyPlayingCardException那麼當你第一次嘗試初始化card變量,然後控制就會傳遞到catch沒有card已經初始化catch塊會打印一條消息,但只允許控制落入該問題的線路。您可以捕獲異常並輸出消息,但在這種情況下,catch塊也必須拋出一個異常(無論是相同還是不同的異常),否則它必須以某種方式初始化card本身。

請注意,如果Card構造函數在內循環的後續迭代中拋出MyPlayingCardException,則card將保留其先前的值。雖然在這種情況下它不會未初始化,但它所具有的價值並不是您實際想要使用的價值。

+0

將originalDecks.add(卡)移至try塊。這解決了問題。 – 2014-12-05 20:53:53

相關問題