2017-02-02 100 views
-4

我寫了一個簡單的紙牌遊戲對。基本上玩家每次選擇2張牌1,看他們是否匹配。如果它們匹配,則所選擇的兩張牌保持朝上,如果它們不匹配,那麼所拾取的兩張牌將恢復到朝下位置。現在我希望遊戲能夠自動播放,當有一對已知的玩家參與時,如果有一張已知的卡片與第一張隨機挑選的卡片相匹配,則翻轉該卡片。否則隨機翻轉。我不斷收到錯誤variable might not have been initializedJava錯誤變量可能未被初始化

PlayCard.java:133: error: variable Num1 might not have been initialized 
       g.flip(Num1); 
        ^
PlayCard.java:143: error: variable Num1 might not have been initialized 
          if (g.cardArray[i].value == g.cardArray[Num1-1].value){ 
                    ^
PlayCard.java:152: error: variable Num1 might not have been initialized 
         }while (Num2 == Num1 || g.cardArray[Num2-1].flipped == true); 
             ^
PlayCard.java:155: error: variable Num2 might not have been initialized 
       g.flip(Num2); 
        ^
PlayCard.java:164: error: variable Num1 might not have been initialized 
       g.cardArray[Num1-1].flipped = false; 
          ^
PlayCard.java:166: error: variable Num2 might not have been initialized 
       g.cardArray[Num2-1].flipped = false; 
          ^
6 errors 

這裏是我的特定方法的代碼...

public static int playGood(MatchCardGame g){ 
    int gameSize = g.n; 
    long seed = System.currentTimeMillis(); 
    Random rand = new Random(seed); 
    int Num1, Num2; 

    while (!g.gameOver()){ 

     // during even number of flipCounts 
     if (g.getFlips()%2 == 0){ 

      // if there's a known pair, flip the pair 
      for (Card card : g.cardArray){ 
       if (card.valueKnown == true){ 
        for (int i = 0; i < gameSize; i++){ 
         if (g.cardArray[i] == g.cardArray[i+1]){ 
          Num1 = i; 
          Num2 = i + 1; 
         }else{ 
          for (int j = i+2; j < gameSize-2; j++){ 
           if (g.cardArray[i] == g.cardArray[j]){ 
            Num1 = i; 
            Num2 = j; 
           } 
          } 
         } 
        } 
       }else{ 
         // if no pair, flip a random valid card 
         do{ 
          Num1 = rand.nextInt(gameSize)+1; 
         }while (g.cardArray[Num1-1].flipped == true); 
        } 
      } 
      g.flip(Num1); 
     } 

     // during odd number of flipCounts 
     if (g.getFlips()%2 != 0){ 

      // if there's a known value matches the first card, flip that card 
      for (Card card : g.cardArray){ 
       if (card.valueKnown == true){ 
        for (int i = 0; i < gameSize; i++){ 
         if (g.cardArray[i].value == g.cardArray[Num1-1].value){ 
          Num2 = i; 
         } 
        } 
       }else{ 

        // if no known value matches card1, flip a random card 
        do { 
         Num2 = rand.nextInt(gameSize)+1; 
        }while (Num2 == Num1 || g.cardArray[Num2-1].flipped == true); 
       } 
      } 
      g.flip(Num2); 
     } 

     // compare two cards, if match, cards remain flipped 
     g.wasMatch(); 
     if (g.wasMatch() == true){}else{ 

      // if mismatch, cards revert back to unflipped, and store the value as known 
      g.flipMismatch(); 
      g.cardArray[Num1-1].flipped = false; 
      g.cardArray[Num1-1].valueKnown = true; 
      g.cardArray[Num2-1].flipped = false; 
      g.cardArray[Num2-1].valueKnown = true; 
     } 
    } 
    return g.getFlips(); 
} 
+0

'INT NUM1 = 0; int Num2 = 0;'嘗試用這個 – Gatusko

+2

java中的局部變量(例如:'int Num1;')應該在使用它們之前初始化。它們沒有其他編程語言可能具有的默認值/垃圾值 –

+0

在您的'while(!g.gameOver())'循環中使用它們之前,將'num1'和'num2'初始化爲'0'。試試這個:'int Num1 = 0,Num2 = 0'; – Chip

回答

0

Java是無法知道這些值的if語句內部設置,所以你會需要將它們初始化爲一些值。確保代碼正常工作的一種好方法是將它們設置爲某個虛擬值,然後驗證它們是否被正確設置爲其他值。例如:

int Num1, Num2 = -1; 

然後使用它們雙重檢查之前就已經設置爲有效的值> = 0