0

让我们开始谈正事吧。我有这行代码:

    public static Tile[] tiles = new Tile[400];

这在 eclipse 中给了我以下错误

    Syntax error on token ";", { expected after this token

Tile 是同一个项目和包中的一个类,我想为其创建一个数组,因为我想不出任何其他方法来完成程序其余部分中的事情......

我在谷歌和stackoverflow上看过,但找不到答案。有人可以向我解释我做错了什么吗?

在此先感谢,德克。

编辑:这是瓷砖类:

    package com.mrdls.invade;

    public class Tile {

        public String name = "";
        public int id = 0;
        public boolean passable = false;

        public Tile(String name, int id){
            this.name = name;
            this.id = id;

            if(name == "air"){
                passable = true;
            }
        }
    }

EDIT2,这里是 TilesBlocks 类

    package com.mrdls.invade;

    import java.awt.image.BufferedImage;

    import javax.imageio.ImageIO;

    public class TilesBlocks {

        public static BufferedImage tilesetBlocks;

        public static int pixelsPerTile = 20;

        public int[] ints = new int[20];

        public Tile[] tiles = new Tile[400];

        tiles[0] = new Tile("air", -1);
        /*public static final Tile earth = new Tile("earth", 0);
        public static final Tile stone = new Tile("stone", 1);
        public static final Tile cobblestone = new Tile("cobblestone", 2);
        public static final Tile sand = new Tile("sand", 3);
            public static final Tile sandstone = new Tile("sandstone", 4);
        public static final Tile grass = new Tile("grass", 5);
        public static final Tile woodLog = new Tile("log", 6);
        public static final Tile leaves = new Tile("leaves", 7);
        public static final Tile pumpkin = new Tile("pumpkin", 8);
        public static final Tile melon = new Tile("melon", 9);
        public static final Tile pumpkinCarved = new Tile("carved pumpkid", 10);

        public static final Tile copperOre = new Tile("copper ore", 20);
        public static final Tile tinOre = new Tile("tin ore", 21);
        public static final Tile ironOre = new Tile("iron ore", 22);
        public static final Tile silverOre = new Tile("silver ore", 23);
        public static final Tile goldOre = new Tile("gold ore", 24);

        public static final Tile diamondOre = new Tile("diamond ore", 40);
        public static final Tile emeraldOre = new Tile("emerald ore", 41);
        public static final Tile rubyOre = new Tile("ruby ore", 42);
        public static final Tile sapphireOre = new Tile("sapphire ore", 43);*/

        public TilesBlocks(){
            try{
                TilesBlocks.tilesetBlocks = ImageIO.read(Tile.class.getResourceAsStream("/tileset_terrain.png"));
            }catch(Exception e){

            }
        }
    }
4

3 回答 3

1
        tiles[0] = new Tile("air", -1);

你不能在课堂上这样做。您需要在构造函数中分配它。阅读初始化赋值之间的区别。

字段的初始化属于方法之外,当然是被类封装的。

赋值属于一个方法。

或者你可以像这样初始化:

Tiles []tiles = new Tiles[]{new Tile("air",-1)} ;

并且.equals不使用 ==

于 2012-11-20T16:27:32.213 回答
1

这个完整的类工作正常。我在您的课程和用法中没有看到任何错误。

这是完整的课程

package com.test;

public class Tile {

    public static Tile[] tiles = new Tile[400];

    public String name = "";
    public int id = 0;
    public boolean passable = false;

    public Tile(String name, int id) {
        this.name = name;
        this.id = id;

        if (name.equals("air")) {
            passable = true;
        }
    }
}
于 2012-11-20T16:31:09.077 回答
0

单击红色错误消息,您很可能会看到“抑制”或“清除棉绒标记” Eclipse 有时会记录错误(在您键入时)并且不会清除标记。清除 Lint Markers 并查看错误是否消失

于 2012-11-20T16:17:39.260 回答