对于声明问题?

时间:2014-05-10 23:08:16

标签: java intellij-idea minecraft bukkit

所以我有这些代码,而且我的for循环有问题。

@EventHandler
    public void onPistonExtend(BlockPistonExtendEvent event)
    {
        if (TownStones.blockPistons)
        {
            List<Block> pushedBlocks = event.getBlocks();
            Material theMaterial = null;
            int end = TownStones.blocks;
            boolean done = false;
            if (pushedBlocks != null) {
                for (Iterator localIterator = pushedBlocks.iterator(); localIterator.hasNext(); !done)
                {
                    Block theBlock = (Block)localIterator.next();
                    theMaterial = theBlock.getType();
                    done = false;
                    int i = 0; continue;
                    if (theMaterial == Material.getMaterial(TownStones.blockType[i]))
                    {
                        event.setCancelled(true);
                        done = true;
                    }
                    else if (i > end)
                    {
                        done = true;
                    }
                    i++;
                }
            }
        }
    }

Intellij Idea告诉我!done不是声明

2 个答案:

答案 0 :(得分:5)

那是因为!done不是声明。问题是你的循环:

for (Iterator localIterator = pushedBlocks.iterator(); localIterator.hasNext(); !done)

这读作“开始,声明localIterator并将其设置为pushedBlocks.iterator(),然后在迭代器具有更多元素时继续”。 !done没有语义含义。将for循环重写为:

for (Iterator localIterator = pushedBlocks.iterator(); localIterator.hasNext() && !done;)

答案 1 :(得分:2)

Java for循环必须符合

for (initialization; termination; increment) {
    statement(s)
}

式。 !done不是有效的增​​量语句。请参阅 http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html 有关如何使用for循环的介绍。