【问题标题】:'Cannot find symbol,' but I see nothing wrong“找不到符号”,但我看不出有什么问题
【发布时间】:2014-01-05 23:55:17
【问题描述】:

它找不到我的 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;   
}
}

感谢您的帮助,如果您需要,我可以从我的课程中提供更多代码。

【问题讨论】:

  • 好的,我们都知道问题所在。但是你到底想在这里完成什么。一个简单的全局声明它们可能无法解决问题,所以在我知道你试图用你的程序完全完成什么之前,我不会建议这样做。
  • 我正在尝试将卡片添加到我创建的玩家手中,但它们不在我需要的范围内。

标签: java object find symbols


【解决方案1】:

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

您已在自己的 if 块作用域中声明了每个 Player 变量。在这些之外无法访问它们。

要么在更大的范围内声明它们,例如,在if 块之外,要么对块内的对象执行您需要做的所有事情。

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

【讨论】:

    猜你喜欢
    • 2015-11-01
    • 2022-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多