java - 编译器找不到枚举类型。(战舰游戏)
问题描述
我正在尝试编写战舰游戏。首先,我创建了一个类Tile
来表示将成为战场板的二维数组的一个块:
public class Tile {
public enum type{SEA, SHIP, HIT, MISS};
int x,y;
public type tile_type;
public Tile(int x,int y, type type){
this.x = x;
this.y = y;
this.tile_type = type;
}
}
接下来我创建了一个名为的类Board
,在构造函数中我想让每个图块都具有图块类型SEA
:
public class Board{
static int Dimension = 7;
Tile[][] Matrix = null;
public Board(Tile[][] Matrix){
Matrix = new Tile[Dimension][Dimension];
for(int i=0; i<=Dimension;i++){
for(int u=0;u<=Dimension;u++){
Matrix[i][u].tile_type = type.SEA;
}
}
}
}
这是 Java 编译器在type.SEA中找不到变量类型的地方。
我不明白问题出在哪里。
解决方案
似乎您没有 import type
in Board
,您可以明确添加外部类:
Matrix[i][u].tile_type = Tile.type.SEA;