为我的扫雷代码打印 "labels" 时遇到问题
Having issue with printing out "labels" for my minesweeper code
我正在写扫雷游戏,我正在为 the following method- you can also look at the code below. I pass in a smaller map and I put in a new array but I'm struggling with the logic of how to get the 0 column and 0 row to print out like in this example 苦苦挣扎。你可以在下面看到我的一些代码。尽管我无法真正让它按照我预期的方式工作。
/**
* This prints out the map. This shows numbers of the columns
* and rows on the top and left side, respectively.
* map[0][0] is row 1, column 1 when shown to the user.
* The first column, last column and every multiple of 5 are shown.
*
* To print out a 2 digit number with a leading space if the number
* is less than 10, you may use:
* System.out.printf("%2d", 1);
*
* @param map The map to print out.
*/
public static void printMap(char[][] map) {
char[][] mapWithNum = new char[map.length + 1][map[0].length + 1];
for (int i = 1; i < mapWithNum.length; i++) {
for (int j = 1; j < mapWithNum[0].length; j++) {
mapWithNum[i][j] = map[i - 1][j - 1];
}
}
for (int i = 0; i < mapWithNum.length; i++) {
for (int j = 0; j < mapWithNum[0].length; j++) {
if (i == 0 || j == 0) {
if (i == 0 && j == 0) {
System.out.print(" ");
} else if (j == 1 && i == 0) {
System.out.print(" " + j);
} else if ((j) % 5.0 == 0) {
System.out.print(" t1" + (j));
if (j == map.length - 1 && i == 0) {
System.out.print(" t2" + (j + 1));
}
} else {
System.out.print("--");
if (j == map.length - 1 && i == 0) {
System.out.print(" t2" + (j + 1));
}
}
} else {
System.out.print(" " + mapWithNum[i][j]);
}
/* if (j == 1) {
if (j % 5 == 0) {
System.out.print(" " + (i + 1));
}
else {
System.out.print('-');*/
//}
// }
}
System.out.println();
}
return;
}
如有任何帮助,我们将不胜感激。
您不需要在您的方法中创建新的二维数组。只需循环原来的那个。 printMap() 方法应该做的第一件事是打印初始 header 行:" 1------ 5--------10"
。这很简单,即使您必须使其动态化。接下来,正常循环遍历二维数组。在打印每一行之前,打印行前缀。例如,数组第一行的前缀是 " "
。第二行的前缀是 " 1 "
。等等。请注意,第 0、4、9 行...有不同的情况。其他行的前缀只是 " | "
。使用 printf() 进行格式化会有所帮助。您应该在进入内部循环之前打印前缀。还要记住不要打印带有前缀的换行符。您的内部循环应该从前缀停止的地方继续。最后,内部循环应该打印数组的每个元素。但是记得在打印的时候给每个元素加上一个" "
。我建议先尝试只打印点。然后添加 header 行,最后添加行前缀。
我正在写扫雷游戏,我正在为 the following method- you can also look at the code below. I pass in a smaller map and I put in a new array but I'm struggling with the logic of how to get the 0 column and 0 row to print out like in this example 苦苦挣扎。你可以在下面看到我的一些代码。尽管我无法真正让它按照我预期的方式工作。
/**
* This prints out the map. This shows numbers of the columns
* and rows on the top and left side, respectively.
* map[0][0] is row 1, column 1 when shown to the user.
* The first column, last column and every multiple of 5 are shown.
*
* To print out a 2 digit number with a leading space if the number
* is less than 10, you may use:
* System.out.printf("%2d", 1);
*
* @param map The map to print out.
*/
public static void printMap(char[][] map) {
char[][] mapWithNum = new char[map.length + 1][map[0].length + 1];
for (int i = 1; i < mapWithNum.length; i++) {
for (int j = 1; j < mapWithNum[0].length; j++) {
mapWithNum[i][j] = map[i - 1][j - 1];
}
}
for (int i = 0; i < mapWithNum.length; i++) {
for (int j = 0; j < mapWithNum[0].length; j++) {
if (i == 0 || j == 0) {
if (i == 0 && j == 0) {
System.out.print(" ");
} else if (j == 1 && i == 0) {
System.out.print(" " + j);
} else if ((j) % 5.0 == 0) {
System.out.print(" t1" + (j));
if (j == map.length - 1 && i == 0) {
System.out.print(" t2" + (j + 1));
}
} else {
System.out.print("--");
if (j == map.length - 1 && i == 0) {
System.out.print(" t2" + (j + 1));
}
}
} else {
System.out.print(" " + mapWithNum[i][j]);
}
/* if (j == 1) {
if (j % 5 == 0) {
System.out.print(" " + (i + 1));
}
else {
System.out.print('-');*/
//}
// }
}
System.out.println();
}
return;
}
如有任何帮助,我们将不胜感激。
您不需要在您的方法中创建新的二维数组。只需循环原来的那个。 printMap() 方法应该做的第一件事是打印初始 header 行:" 1------ 5--------10"
。这很简单,即使您必须使其动态化。接下来,正常循环遍历二维数组。在打印每一行之前,打印行前缀。例如,数组第一行的前缀是 " "
。第二行的前缀是 " 1 "
。等等。请注意,第 0、4、9 行...有不同的情况。其他行的前缀只是 " | "
。使用 printf() 进行格式化会有所帮助。您应该在进入内部循环之前打印前缀。还要记住不要打印带有前缀的换行符。您的内部循环应该从前缀停止的地方继续。最后,内部循环应该打印数组的每个元素。但是记得在打印的时候给每个元素加上一个" "
。我建议先尝试只打印点。然后添加 header 行,最后添加行前缀。