开发者

scope of variables when using for-loop in java - eclipse / compiler error?

I've written the following code:

for(int layer = 0; layer <countLayers; layer++);
{
    List<Sprite> spritesInLayer = sceneGraph.getLayer(layer);
}

when i compile this snippet, I get an error, that in the line within the for-Loop, eclipse complains that 'layer' is an unknown symbol [... = sceneGraph.getLayer(layer);] and wants me to introduce the field / variable / ... 'layer'.

But when using this snippet, it works.

int layer = 0;
for(layer = 0; layer <countLayers; layer++);
{
    List<Sprite> spritesInLayer = sceneGraph.getLayer(layer);
}

does anybody know, what I'm missing in the first code? Or might this be some kind of a bug of eclipse / the java compiler?

开发者_开发百科I'm using Java 6 JDK Update 20 64 bit on Win 7 64-bit Home Premium and Eclipse Helios 64-bit (build 20100617-1415)


Change

for(int layer = 0; layer <countLayers; layer++);

to

for(int layer = 0; layer <countLayers; layer++)

The spurious semicolon means that the for loop has an empty body. The following {....} is interpreted as a separate statement. And of course, layer is out of scope within that block.


delete the semicolon after the for line! The contents of the braces are not looped in your example, and therefore layer is undefined...

That's why eclipse is useful!


Please remove semicolon ";" from following line.

for(int layer = 0; layer < countLayers; layer++);

for statement doesn't require ;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜