英文:
How can I access the Cell Array in my getCell method? (Java)
问题
以下是您要翻译的内容:
我的任务是实现康威的生命游戏。为此,我需要创建一个名为GameMap的类。在这个类中,我将初始化一个二维数组。我会使用以下两个方法:
private static Cell[][] buildCellArray(int width, int height){
Cell[][] cellArray = new Cell[width][height];
int i;
int j;
for(i = 0; i < width; i++) {
for(j = 0; j < height; j++) {
cellArray[i][j] = new Cell();
}
}
return cellArray;
}
public GameMap(int sizeX, int sizeY) {
buildCellArray(sizeX, sizeY);
}
现在,我想通过getCell(int posX, int posY)方法访问cellArray中的特定Cell。我的问题是如何访问cellArray?我想这样访问它:
public Cell getCell(int posX, int posY){
return cellArray[posX][posY];
}
以便我可以获取特定位置的Cell。我希望有人能帮助我解决问题。
因此,完整的代码部分如下:
public class GameMap {
private static Cell[][] buildCellArray(int width, int height){
Cell[][] cellArray = new Cell[width][height];
int i;
int j;
for(i = 0; i < width; i++) {
for(j = 0; j < height; j++) {
cellArray[i][j] = new Cell();
}
}
return cellArray;
}
public GameMap(int sizeX, int sizeY) {
buildCellArray(sizeX, sizeY);
}
public Cell getCell(int posX, int posY){
return cellArray[posX][posY];
}
}
但是,IDE指出getCell方法中的cellArray不是一个变量。
英文:
my Task is to make an implementation of Conway's Game of Life. Therefor I need to create the class GameMap. In this class I will initialize an 2D Array.
Therefor I use those two methods.
private static Cell[][] buildCellArray(int width, int height){
Cell[][] cellArray = new Cell[width][height];
int i;
int j;
for(i = 0; i < width; i++) {
for(j = 0; j < height; j++) {
cellArray[i][j] = new Cell();
}
}
return cellArray;
}
public GameMap(int sizeX, int sizeY) {
buildCellArray(sizeX, sizeY);
}
Now I want to access the cellArray to access a special Cell with the getCell(int posX, int posY) method.
My question is how I can access the cellArray?
I wanted to access it like this:
public Cell getCell(int posX, int posY){
return cellArray[posX][posY];
}
So that I get the Cell at a special position.
I hope somebody can help me out.
So the complete code part is:
public class GameMap {
private static Cell[][] buildCellArray(int width, int height){
Cell[][] cellArray = new Cell[width][height];
int i;
int j;
for(i = 0; i < width; i++) {
for(j = 0; j < height; j++) {
cellArray[i][j] = new Cell();
}
}
return cellArray;
}
public GameMap(int sizeX, int sizeY) {
buildCellArray(sizeX, sizeY);
}
public Cell getCell(int posX, int posY){
return cellArray[posX][posY];
}
}
And the IDE says that cellArray in the method getCell is not a variable.
答案1
得分: 0
以下是翻译好的部分:
IDE显示cellArray
无法解析为变量,因为它是局部变量,要解决这个问题,只需将Cell[][] cellArray = new Cell[width][height];
移到buildCellArray()
之外。
public class GameMap {
Cell[][] cellArray;
private static Cell[][] buildCellArray(int width, int height){
int i;
int j;
for(i = 0; i < width; i++) {
for(j = 0; j < height; j++) {
cellArray[i][j] = new Cell();
}
}
return cellArray;
}
public GameMap(int sizeX, int sizeY) {
buildCellArray(sizeX, sizeY);
}
public Cell getCell(int posX, int posY){
return cellArray[posX][posY];
}
}
英文:
The IDE says cellArray
cannot be resolve to a variable because it is local variable, to pass this problem just move Cell[][] cellArray = new Cell[width][height];
outside the buildCellArray()
.
public class GameMap {
Cell[][] cellArray;
private static Cell[][] buildCellArray(int width, int height){
int i;
int j;
for(i = 0; i < width; i++) {
for(j = 0; j < height; j++) {
cellArray[i][j] = new Cell();
}
}
return cellArray;
}
public GameMap(int sizeX, int sizeY) {
buildCellArray(sizeX, sizeY);
}
public Cell getCell(int posX, int posY){
return cellArray[posX][posY];
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论