'找不到符号',但我没有看错

时间:2013-12-18 00:21:44

标签: java object find symbols

它无法找到我的Player对象,但我宣布了它们......当我尝试添加卡片时,什么会修复我的错误?

这是主要课程的相关部分:

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;   
}
}

我感谢任何帮助,如果您需要,我可以提供更多代码。

1 个答案:

答案 0 :(得分:1)

由大括号{}分隔的每个代码块都定义了一个范围。在该范围内声明的任何命名实体只能在声明后在该范围内访问。

您已在自己的Player块范围内声明了每个if变量。它们不在那些之外。

在更大的范围内声明它们,例如,在if块之外,或者执行块中对象所需的一切。

Here's对这种现象的另一种描述。