2013-12-18 56 views
1

找不到我的播放器對象,但我宣佈了它們...當我嘗試添加卡片時,什麼會解決我的錯誤?'找不到符號',但我沒有看到任何錯誤

這裏是主類的相關部分:

while (something2.equals("yes") || playercount < 2) //Add players to game 
     { 
      System.out.println("Would a(nother) player like to join?"); 
      something2 = scan.nextLine(); 
      System.out.println(); 
      if (something2.equals("yes")) 
      { 
       if (playercount <= 4) 
       { 
        if (playercount == 0) 
        { 
         System.out.println("What is your name: "); 
         Player one = new Player(scan.nextLine()); 
         playercount++; 
         System.out.println(); 
        } 
        else if (playercount == 1) 
        { 
         System.out.println("What is your name: "); 
         Player two = new Player(scan.nextLine()); 
         playercount++; 
         System.out.println(); 
        } 
        else if (playercount == 2) 
        { 
         System.out.println("What is your name: "); 
         Player three = new Player(scan.nextLine()); 
         playercount++; 
         System.out.println(); 
        } 
        else if (playercount == 3) 
        { 
         System.out.println("What is your name: "); 
         Player four = new Player(scan.nextLine()); 
         playercount++; 
         System.out.println(); 
        } 
        else {System.out.println("Only four players are allowed."); 
          something2 = "no";} 
       } 
      } 
      else if (playercount < 2) 
      { 
       System.out.println("You need at least two players..."); 
       System.out.println(); 
      } 
      else something2 = "no"; 
     } 
     //Deal cards 
     if (playercount == 2) 
     { 
      for (int i = 1; i < 8; i++) 
      { 
       one.addCard(Card.draw(deck)); 
       deck = Card.getDeck(); 
       two.addCard(Card.draw(deck)); 
       deck = Card.getDeck(); 
      } 
     } 
     else if (playercount == 3) 
     { 
      for (int i = 1; i < 8; i++) 
      { 
       one.addCard(Card.draw(deck)); 
       deck = Card.getDeck(); 
       two.addCard(Card.draw(deck)); 
       deck = Card.getDeck(); 
       three.addCard(Card.draw(deck)); 
       deck = Card.getDeck(); 
      } 
     } 
     else 
     { 
      for (int i = 1; i < 8; i++) 
      { 
       one.addCard(Card.draw(deck)); 
       deck = Card.getDeck(); 
       two.addCard(Card.draw(deck)); 
       deck = Card.getDeck(); 
       three.addCard(Card.draw(deck)); 
       deck = Card.getDeck(); 
       four.addCard(Card.draw(deck)); 
       deck = Card.getDeck(); 
      } 
     } 
    } 

我的播放器類:

import java.util.*; 

public class Player 
{ 
private static String name; 
private static Card[] hand = new Card[52]; 
private static int handsize = 0; 

//Constructor 
public Player(String n) 
{ 
    name = n; 
} 

//Mutators 
public static void addCard(Card c) 
{ 
    hand[handsize] = c; 
    handsize++; 
} 

//Accessors 
public static String getName() 
{ 
    return name; 
} 
public static Card[] getHand() 
{ 
    return hand; 
} 
} 

我感謝所有幫助,如果你需要它,我可以從我的類提供更多的代碼。

+5

範圍,人,範圍! –

+0

你是什麼意思? – William

+0

好的,我們都知道這個問題。但是你究竟想在這裏完成什麼?一個簡單的全局聲明可能不會訣竅,所以我不會建議,直到我知道你想完全用你的程序完成什麼。 –

回答

1

由花括號{}定義的每個代碼塊都定義了一個範圍。在該範圍內聲明的任何命名實體只能在其聲明後在該範圍內訪問。

您已經在各自的if塊範圍內聲明瞭您的Player變量中的每一個。除此之外,他們無法訪問。

要麼聲明它們在更大的範圍內,例如在if塊之外,要麼執行塊內部對象所需的所有操作。

Here's這種現象的另一種描述。

+0

謝謝。我的老師從未告訴我 - – William