[英]Creating a Java increasing alphabet grid with a 5x5 array
我需要重新创建的网格看起来像这样
ABCDE
T.....F
S.....G
R.....H
Q.....I
P.....J
ONMLK
我现在的网格看起来像这样
ABCDE
0.....0
1.....1
2.....2
3.....3
4.....4
ONMLK
我创建网格的代码
// * Method * Creating the maze grid
public static void createMazeGrid(){
// First section of Maze Grid
System.out.print(" ");
for(char alphabet = 'A'; alphabet < 'F'; alphabet++)
System.out.print(alphabet);
System.out.println();
// Middle section of Maze Grid
// *TRY TO FIGURE OUT ALPHABET GRID*
for(int i = 0; i < maze.length; i++) {
System.out.print(" ");
for (int j = 0; j < maze[i].length; j++) {
maze[i][j] = ".";
if (j == 0)
System.out.print(i + maze[i][j]);
else if (j == maze[i].length - 1)
System.out.print(maze[i][j] + i);
else
System.out.print(maze[i][j]);
}
System.out.println();
}
// Last section of Maze Grid
System.out.print(" ");
for(char alphabet = 'O'; alphabet > 'J'; alphabet--)
System.out.print(alphabet);
System.out.println();
}
我已经在public class中声明了这些变量,不是我的main方法。 我将如何实现这一目标? 我尝试将 map 中间部分的 int 更改为 char,就像我的顶部和底部部分一样,但它只是将 map 的中间部分切掉了。
公共 static int numRows = 5;
公共 static int numCols = 5;
公共 static String[][] 迷宫 = new String[numRows][numCols];
完成代码的一种简单方法是在已有 output 的地方使用字符。
public class Maze {
public static int numRows = 5;
public static int numCols = 5;
public static String[][] maze = new String[numRows][numCols];
// * Method * Creating the maze grid
public static void createMazeGrid(){
// First section of Maze Grid
System.out.print(" ");
for(char alphabet = 'A'; alphabet < 'F'; alphabet++)
System.out.print(alphabet);
System.out.println();
// Middle section of Maze Grid
// *TRY TO FIGURE OUT ALPHABET GRID*
for(int i = 0; i < maze.length; i++) {
System.out.print(" ");
for (int j = 0; j < maze[i].length; j++) {
maze[i][j] = ".";
if (j == 0)
System.out.print((char)('T' - i) + maze[i][j]); // left side
else if (j == maze[i].length - 1)
System.out.print(maze[i][j] + (char)('F' + i)); // right side
else
System.out.print(maze[i][j]);
}
System.out.println();
}
// Last section of Maze Grid
System.out.print(" ");
for(char alphabet = 'O'; alphabet > 'J'; alphabet--)
System.out.print(alphabet);
System.out.println();
}
public static void main(String[] args) { createMazeGrid(); }
}
现在让我们测试一下:
$ java Maze.java
ABCDE
T.....F
S.....G
R.....H
Q.....I
P.....J
ONMLK
看起来挺好的。 :-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.