我需要重新創建的網格看起來像這樣
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();
}
我已經在公共類中宣告了這些變數,而不是我的主要方法。我將如何實作這一目標?我嘗試將地圖中間部分的 int 更改為像我的頂部和底部部分一樣的字符,但它只是將地圖的中間部分切掉。
公共靜態 int numRows = 5;
公共靜態 int numCols = 5;
public static String[][] maze = new String[numRows][numCols];
uj5u.com熱心網友回復:
完成代碼的一種簡單方法是在已有輸出的地方使用字符。
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
看起來挺好的。:-)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/469094.html
上一篇:回圈時動態更改串列
下一篇:二值影像中所有可能的組合
