문제

So I have this bit of code and I'm having problems with my for loop.

@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 is telling me !done is not a Statement

도움이 되었습니까?

해결책

That is because !done isn't a statement. The issue is your for loop:

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

This reads as "to begin, declare localIterator and set it to pushedBlocks.iterator(), then continue while the iterator has more elements". There's no semantic meaning to !done. Rewrite the for loop as:

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

다른 팁

A Java for loop must conform to the

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

style. !done is not a valid increment statement. Please refer to http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html for an introduction on how to use a for loop.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top