让我们开始做生意吧。我有这行代码:
public static Tile[] tiles = new Tile[400];
在eclipse中给出了以下错误
Syntax error on token ";", { expected after this token
Tile是同一个项目和程序包中的一个类,我想创建一个数组,因为我无法想到在程序的其余部分做任何其他事情......
我已经看过google和stackoverflow,但无法找到答案。有人可以向我解释我做错了吗?
提前致谢, 德克。
编辑:这是Tile类:
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){
}
}
}
答案 0 :(得分:1)
tiles[0] = new Tile("air", -1);
你不能在班上这样做。您需要在构造函数中分配它。 阅读初始化和分配之间的区别。
字段的初始化属于方法之外,但当然是由类封装。
作业属于一种方法。
或者你可以这样初始化:
Tiles []tiles = new Tiles[]{new Tile("air",-1)} ;
并使用.equals
not ==
答案 1 :(得分: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;
}
}
}
答案 2 :(得分:0)
单击红色错误消息,很可能您将看到'抑制'或'清除lint标记'Eclipse有时会记录错误(当您键入时)并且不会清除标记。 清除Lint Markers并查看错误是否消失