【问题标题】:Static array declared but method cannot work声明了静态数组但方法不起作用
【发布时间】:2014-11-04 12:10:57
【问题描述】:

我必须在Greenfoot 做我的作业。 这部分意味着我必须保存Chess的位置,然后点击重置。
然后,我必须选择加载将棋子放回重置前的位置。
由于我不知道数组的确切大小,我知道我可以使用List,但在作业中是不允许的。

屏幕上没有显示任何内容,但没有错误消息。

假设我有一个名为 Chess 的类。

static Actor[] allChess;

public void save() // this is the save
{
    Actor[] allChess = GWorld.getAllObjects("Chess");
}

public void load() // this is the load
{
    if (allChess != null)
    {
        for (int i=0; i < allChess.length; i++)
        {
            Chess ch = (Chess) allChess[i];
            GWorld.addOneObject(new Chess(ch.color, ch.rank), ch.getX(), ch.getY());
        }
    }
}

非常感谢!

【问题讨论】:

  • Chess 是 Actor 的子类 这整个块在 World 中声明。

标签: java arrays methods greenfoot


【解决方案1】:

allChesssave() 中被重新声明为局部变量。这样做:

public void save() // this is the save
{
     allChess = GWorld.getAllObjects("Chess");
}

【讨论】:

    猜你喜欢
    • 2014-05-12
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 2012-08-19
    • 2016-02-16
    相关资源
    最近更新 更多