2013-12-18 11 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

중괄호로 구분 된 모든 코드 블록 {}은 범위를 정의합니다. 해당 범위에서 선언 된 모든 명명 된 엔터티는 선언 후에 해당 범위에서만 액세스 할 수 있습니다.

Player 개의 변수를 각각 자신의 if 블록 범위에 선언했습니다. 그들은 그것들 밖에서 접근 할 수 없다.

더 큰 범위 (예 : if 블록 외부)에서 선언하거나 블록 내부의 개체에 대해 필요한 모든 작업을 수행하십시오.

Here's이 현상에 대한 다른 설명.

+0

. 내 선생님은 내게 그렇게 말하지 않았다. – William