我正在尝试为Java中的jrpg创建一个映射,我正在作为一个学习项目。我在名为map的地形对象上创建了一个10x10的数组,并创建了一个独立于数组的地形对象作为测试。接下来,我尝试为 Map 2D 数组的每次迭代创建一个 terrain 对象。在这里我得到错误 "地形无法解析为变量" 我尝试移动了几行,并做了30分钟或更长时间的Google搜索以寻找解决方案。任何帮助或提示将不胜感激。
terrain[ ][ ] map = new terrain[10][10];
terrain T1 = new terrain();
public void mapp(){
int a = RPGmain.test.game.map.length;
int c=0;
int d=0;
while (c <= a){
int b = RPGmain.test.game.map[c].length;
while(d <= b){
terrain map[c][d]= new terrain(); // <------ the first terrain in this line is where i get the error
d++;
}
d=0;
c++;
}
}
声明字段或变量时,需要指定其类型:
int d=0;
当你赋值给已经定义的变量时,你只需要该变量的名称和新值:
d=0;
数组也是如此。您已将映射声明为:
terrain[ ][ ] map = new terrain[10][10];
分配时无需指定类型:
map[c][d]= new terrain();
你只需要:
map[c][d]= new terrain();
当您知道需要多少次迭代时,最好使用for
循环:
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
map[i][j] = new Terrain();
}
}
另请注意,按照惯例,类的名称以大写字母开头。如果你忽略这一点,编译器不会抱怨,但当你将来调试你的代码时,你会感谢自己。