對于這個程式,我應該添加兩種不同型別的“鏡像”(正斜杠和反斜杠代表我游戲中的鏡像),即“/”和“\”。我已經成功地將第一組斜線添加到我的迷宮陣列中,但我似乎無法弄清楚如何同時添加兩個不同的斜線。
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.");
}
uj5u.com熱心網友回復:
我認為您只需要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] = "/";
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/469092.html
上一篇:為什么在操作python陣列時需要[:]?(旋轉問題)
下一篇:回圈時動態更改串列
