英文:
Replace elements in a print statement
问题
以下是翻译后的内容:
所以我有一个字符串二维数组,用它打印出一个游乐场:
private final int rowX = 10;
private final int colY = 10;
private final String[][] playGroundArray = new String[rowX][colY];
然后我使用循环将一个字符数组添加到我的playGroundArray中的10个随机位置。
mines[0] = "*";
for (int i = 0; i < numberOfMines; i++) {
getPlayGroundArray()[random.nextInt(playGroundArray.length - 2) + 1][random.nextInt(playGroundArray.length - 2) + 1] = Arrays.deepToString(mines);
}
这样一切都运行得很好。
现在我将其打印出来。
我得到了这个:
[[ , 0, 1, 2, 3, 4, 5, 6, 7, ], [0, [], +, [], +, +, +, +, [], ], [1, +, +, +, +, +, +, +, +, ], [2, +, +, +, [], +, +, +, [], ], [3, +, +, +, [], +, +, +, +, ], [4, +, [], +, +, +, +, +, +, ], [5, +, [], +, +, +, +, +, +, ], [6, +, +, +, +, +, +, +, +, ], [7, +, +, +, +, +, +, +, +, ], [ , , , , , , , , , ]]
我现在尝试使用`.replace`方法来构建我的游乐场,使其看起来像一个游乐场:
System.out.println(Arrays.deepToString(playGround.getPlayGroundArray())
.replace("[[", " ")
.replace("]]", " \n")
.replace("], [", " \n")
.replace("]", " ")
.replace(",", " ")
.replace("[", " "));
不是我想要的:P
0 1 2 3 4 5 6 7
0 + + + + + *
1 + * + + + + + +
2 * + + + + + + +
3 + * + + + + + +
4 + * + + + + * +
5 + + + + + + + *
6 + + + + + + + +
7 + + * + + + + +
因此,结果应该看起来像这样:
0 1 2 3 4 5 6 7
0 + * * + + + + +
1 + + + + * + + +
2 + + * + + + + *
3 * + + + + + + +
4 + + * + + * * +
5 + + + + + + + +
6 + + + + + + * +
7 + + + + + + + +
我最接近的结果是上面的代码。
也许有其他解决方案,因为我尝试了很多方法,但数组的括号是一样的,所以你无法在不改变字符串数组括号的情况下替换字符数组的括号。
每隔10个元素,应该换行。
谢谢你的帮助 :)
英文:
So i got this String 2Dimensional array that prints me out a playGround :
private final int rowX = 10;
private final int colY = 10;
private final String[][] playGroundArray = new String[rowX][colY];
Then i added with a loop to 10 random positions in my playGroundArray, an char array.
mines[0] = "*";
for (int i = 0; i < numberOfMines; i++) {
getPlayGroundArray()[random.nextInt(playGroundArray.length - 2) + 1][random.nextInt(playGroundArray.length - 2) + 1] = Arrays.deepToString(mines);
}
so that all works fine.
Now i print it out.
I get this:
[[ , 0, 1, 2, 3, 4, 5, 6, 7, ], [0, [*], +, [*], +, +, +, +, [*], ], [1, +, +, +, +, +, +, +, +, ], [2, +, +, +, [*], +, +, +, [*], ], [3, +, +, +, [*], +, +, +, +, ], [4, +, [*], +, +, +, +, +, +, ], [5, +, [*], +, +, +, +, +, +, ], [6, +, +, +, +, +, +, +, +, ], [7, +, +, +, +, +, +, +, +, ], [ , , , , , , , , , ]]
I try now with the .replace method to build my playGround so it looks like a playGround:
System.out.println(Arrays.deepToString(playGround.getPlayGroundArray())
.replace("[[", " ")
.replace("]]", " \n")
.replace("], [", " \n")
.replace("]", " ")
.replace(",", " ")
.replace("[", " "));
Not really that what i want
0 1 2 3 4 5 6 7
0 + + + + + *
* +
1 + * + + + + + +
2 * + + + + + + +
3 + * + + + + + +
4 + * + + + + * +
5 + + + + + + + *
6 + + + + + + + +
7 + + * + + + + +
So the result should look like that
0 1 2 3 4 5 6 7
0 + * * + + + + +
1 + + + + * + + +
2 + + * + + + + *
3 * + + + + + + +
4 + + * + + * * +
5 + + + + + + + +
6 + + + + + + * +
7 + + + + + + + +
The closest i came was with the code above.
Is there maybe an other solution, cause i tried many ways, but the array braces they are the same, so u cant replace the char arrays braces without changing the braces from the String array as well.
After every 10th element it should make a new line.
Thanks for your help
答案1
得分: 1
以下是翻译好的内容:
你不应该使用字符串来表示游乐场 - 使用布尔值会更容易,并且不要在枚举本身中包含坐标:
import java.util.*;
public class Playground {
private boolean mines[][];
private int rows;
private int cols;
public Playground(int rows, int cols, int mineCount) {
this.rows = rows; this.cols = cols;
mines = new boolean[rows][cols];
Random rng = new Random();
while (mineCount > 0) {
int r = rng.nextInt(rows);
int c = rng.nextInt(cols);
if (!hasMine(r, c)) { // 如果已经有地雷,则重新放置
mines[r][c] = true;
mineCount--;
}
}
}
boolean hasMine(int row, int col) {
return mines[row][col];
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(" "); // 以3个空格开头
for (int c = 0; c < cols; c++) {
sb.append(String.format("%3d", c)); // 使用空格填充数字左侧
}
for (int r = 0; r < rows; r++) {
sb.append(String.format("\n%3d", r));
for (int c = 0; c < cols; c++) {
sb.append(String.format("%3s", (hasMine(r, c) ? "*" : "+")));
}
}
return sb.append("\n").toString();
}
public static void main(String... args) {
System.out.println(new Playground(10, 10, 10));
}
}
正则表达式很棒,实际上你几乎可以使用大量的正则表达式来完成任何事情。这并不意味着它们适用于所有工作。
英文:
You should not be using strings to represent a playground - it would be much easier to use booleans, and to not include the coordinates in the enumeration itself:
import java.util.*;
public class Playground {
private boolean mines[][];
private int rows;
private int cols;
public Playground(int rows, int cols, int mineCount) {
this.rows = rows; this.cols = cols;
mines = new boolean[rows][cols];
Random rng = new Random();
while (mineCount > 0) {
int r = rng.nextInt(rows);
int c = rng.nextInt(cols);
if ( ! hasMine(r, c)) { // retries placement if mine already there
mines[r][c] = true;
mineCount --;
}
}
}
boolean hasMine(int row, int col) {
return mines[row][col];
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(" "); // starts with 3 spaces
for (int c=0; c<cols; c++) {
sb.append(String.format("%3d", c)); // left-pads a number with spaces
}
for (int r=0; r<rows; r++) {
sb.append(String.format("\n%3d", r));
for (int c=0; c<cols; c++) {
sb.append(String.format("%3s", (hasMine(r, c) ? "*" : "+")));
}
}
return sb.append("\n").toString();
}
public static void main(String ... args) {
System.out.println(new Playground(10, 10, 10));
}
}
Regular expressions are great, and you can actually do almost anything just with lots and lots of regular expressions. This does not mean that they are a good tool for all jobs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论