所以我试图将cellType添加到2D数组中。来自文件的输入看起来像一个6x6文件,其中可以是下面我的枚举类中的枚举类型的任意组合。由于某些原因,当我尝试对程序进行故障排除时,只有WALL被添加到2D阵列中。我在想尝试遍历2D数组的地方可能有错误,但是看不到。
这是我要在2D数组中添加它的地方
MazeCell.CellType[][] cell;
int rows = 6;
int cols = 6;
MazeCell.CellType cell2Add;
while(inputFile.hasNext())
{
String mazeStart = inputFile.nextLine().trim();
String [] mazeRowsAndCols = mazeStart.split(" ");
//System.out.println(mazeStart);
//System.out.println(mazeRowsAndCols[2]);
MazeCell.CellType cell2Add;
for(int r = 1; r < rows+1; r++)
{
System.out.print(r-1);
for(int c = 1; c<cols+1; c++)
{
if(mazeRowsAndCols[r-1].equals("W"))
{
cell2Add = MazeCell.CellType.WALL;
}
else if(mazeRowsAndCols[r-1].equals("M"))
{
cell2Add = MazeCell.CellType.START;
}
else if (mazeRowsAndCols[r-1].equals("C"))
{
cell2Add = MazeCell.CellType.END;
}
else if (mazeRowsAndCols[r-1].equals("O"))
{
cell2Add = MazeCell.CellType.OPEN;
}
else if (mazeRowsAndCols[r-1].equals(" "))
{
cell2Add = MazeCell.CellType.CURRENT_PATH;
}
else if (mazeRowsAndCols[r-1].equals("S"))
{
cell2Add = MazeCell.CellType.END_FOUND;
}
else if (mazeRowsAndCols[r-1].equals("X"))
{
cell2Add = MazeCell.CellType.REJECTED;
}
System.out.print(c);
cell[r-1][c-1] = cell2Add;
}
System.out.println();
}
}
}
inputFile.close()
这是我的枚举类。
public class MazeCell
{
public static enum CellType
{
WALL("W"), START("M"), END("C"), OPEN("O"), CURRENT_PATH(" "), END_FOUND("S"), REJECTED("X");
private final String display;
private String type;
CellType(String display)
{
this.display = display;
}
public String getDisplay() { return display;}
public void setType(String type){this.type = type;}
};
输入文件的外观:
W W W W W W W W M W W W W W O O O W W O W O O W W W C O O W W W W W W W W W W W W W W
在cel [] []内,我似乎得到的只是Wall,
答案 0 :(得分:0)
(mazeRowsAndCols[r-1].equals("S"))
您正在使用OUTER索引遍历INNER元素(输入行长)。试试
(mazeRowsAndCols[c-1].equals("S"))
此外,我更愿意使用switch
(是的,可以使用字符串)。
此外,我不喜欢外部for
,因为它应该受输入行的限制,所以while
应该足够多。
所以实际上应该是这样的:
int row=0;
while(input.hasNext()){
String line=input.nextLine();
String parts[]=input.split(" ");
for(int c=0;c<parts.length;c++){
element=parts[c];
switch(element){
case W: enumToAdd=WALL; break;
case ......
///////////do the magic with `switch` here
}
maze[r][c]=enumToAdd;
}
row++;
}
答案 1 :(得分:0)
public static CellType[][] read(Scanner scan) {
final int totalRows = 6;
final int totalColumns = 6;
CellType[][] maze = new CellType[totalRows][totalColumns];
for (int row = 0; row < totalRows; row++) {
String line = scan.nextLine();
for (int col = 0; col < totalColumns; col++)
maze[row][col] = CellType.parseId(line.charAt(col));
}
return maze;
}
public enum CellType {
WALL('W'),
START('M'),
END('C'),
OPEN('O'),
CURRENT_PATH(' '),
END_FOUND('S'),
REJECTED('X');
private final char id;
CellType(char id) {
this.id = id;
}
public static CellType parseId(char id) {
for (CellType type : values())
if (type.id == id)
return type;
throw new EnumConstantNotPresentException(CellType.class, String.valueOf(id));
}
}
答案 2 :(得分:0)
我建议进行一些更改,以使其更容易推理。
首先,让我们修正一下单元格类型以使其更易于使用:
static
fromDisplay(String s)
方法。setType
,因为对setType
的任何调用都会为该枚举值 everywhere 设置该变量,因为只有一个枚举值的实例。结果代码为
public class MazeCell
{
public static enum CellType
{
WALL("W"),
START("M"),
END("C"),
OPEN("O"),
CURRENT_PATH(" "),
END_FOUND("S"),
REJECTED("X");
static {
final Map<String, CellType> fDisplay = new HashMap<>();
for(final CellType c : CellType.values()) {
fDisplay.put(c.display, c);
}
fromDisplay = fDisplay;
}
private static final Map<String, CellType> fromDisplay;
private final String display;
CellType(String display)
{
this.display = display;
}
public String getDisplay() { return display; }
public static CellType fromDisplay(final String display) {
return fromDisplay.getOrDefault(display, REJECTED);
}
}
}
现在,让我们修复代码。如所指出的,问题是您每次仅解析第一行。让我们对其进行修复,并对其进行一些清理。
// Read our input in a try-with-resources
try(final File file = ...) {
final Scanner input = new Scanner(file);
// initialize the array
final MazeCell.CellType[][] cells = new MazeCell.CellType[6][6];
// assuming there are 6 rows
for(int r=0; r<6; r++) {
if(!input.hasNext()) throw new RuntimeException("Input file does not match our expectations! Only " + (r) + " lines!");
// assuming the columns are separated by spaces
final String[] cols = input.nextLine().trim().split(" ");
if(cols.length != 6) throw new RuntimeException("Input file does not match our expectations! Row " + (r + 1) + " had " + cols.length + " columns!");
for(int c=0; c<6; c++) {
cells[r][c] = MazeCell.CellType.fromDisplay(cols[c]);
}
}
// print it out or whatever
Arrays.stream(cells).map(Arrays::toString).forEach(System.out::println);
}
应该这样做。希望这会有所帮助!