向 Java 数组中添加超过 1 个事物
Adding more than 1 thing into a Java Array
对于这个程序,我应该添加两种不同的'mirrors'(正斜线和反斜线在我的游戏中代表镜像),即'/'和'\'。我已成功将第一组斜线添加到我的迷宫数组中,但我似乎无法弄清楚如何同时添加两个不同的斜线。
public static void placeMirrors(){
Scanner input = new Scanner(System.in);
// Promopting user for how many mirrors they want
System.out.print("\nHow many mirrors do you want in your maze?");
System.out.print("The maximum number of mirrors you can have is 8. The minimum is 2.\n");
int usersMirrors = input.nextInt();
// Deploying mirrors randomly within the maze
for (int i = 1; i <= usersMirrors;) {
int x = (int)(Math.random() * numRows);
int y = (int)(Math.random() * numCols);
// *FIGURE OUT ADDING BOTH SETS AT ONCE THING*
if((x >= 0 && x < numRows) && (y >= 0 && y < numCols) && (maze[x][y] == ".")) {
maze[x][y] = "\";
i++;
}
}
System.out.println("Mirrors placed.");
}
我认为您只需要在循环内添加第二个 if
。我假设您要成对添加“镜子”。
// Deploying mirrors randomly within the maze
for (int i = 1; i <= usersMirrors;) {
int x = (int)(Math.random() * numRows);
int y = (int)(Math.random() * numCols);
if((x >= 0 && x < numRows) && (y >= 0 && y < numCols) && (maze[x][y].equals( "." ) )) {
maze[x][y] = "\";
i++;
}
x = (int)(Math.random() * numRows);
y = (int)(Math.random() * numCols);
if((x >= 0 && x < numRows) && (y >= 0 && y < numCols) && (maze[x][y].equals( "." ) )) {
maze[x][y] = "/";
}
}
对于这个程序,我应该添加两种不同的'mirrors'(正斜线和反斜线在我的游戏中代表镜像),即'/'和'\'。我已成功将第一组斜线添加到我的迷宫数组中,但我似乎无法弄清楚如何同时添加两个不同的斜线。
public static void placeMirrors(){
Scanner input = new Scanner(System.in);
// Promopting user for how many mirrors they want
System.out.print("\nHow many mirrors do you want in your maze?");
System.out.print("The maximum number of mirrors you can have is 8. The minimum is 2.\n");
int usersMirrors = input.nextInt();
// Deploying mirrors randomly within the maze
for (int i = 1; i <= usersMirrors;) {
int x = (int)(Math.random() * numRows);
int y = (int)(Math.random() * numCols);
// *FIGURE OUT ADDING BOTH SETS AT ONCE THING*
if((x >= 0 && x < numRows) && (y >= 0 && y < numCols) && (maze[x][y] == ".")) {
maze[x][y] = "\";
i++;
}
}
System.out.println("Mirrors placed.");
}
我认为您只需要在循环内添加第二个 if
。我假设您要成对添加“镜子”。
// Deploying mirrors randomly within the maze
for (int i = 1; i <= usersMirrors;) {
int x = (int)(Math.random() * numRows);
int y = (int)(Math.random() * numCols);
if((x >= 0 && x < numRows) && (y >= 0 && y < numCols) && (maze[x][y].equals( "." ) )) {
maze[x][y] = "\";
i++;
}
x = (int)(Math.random() * numRows);
y = (int)(Math.random() * numCols);
if((x >= 0 && x < numRows) && (y >= 0 && y < numCols) && (maze[x][y].equals( "." ) )) {
maze[x][y] = "/";
}
}