替换打印语句中的元素

huangapple go评论96阅读模式
英文:

Replace elements in a print statement

问题

以下是翻译后的内容:

  1. 所以我有一个字符串二维数组用它打印出一个游乐场

private final int rowX = 10;
private final int colY = 10;
private final String[][] playGroundArray = new String[rowX][colY];

  1. 然后我使用循环将一个字符数组添加到我的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);
}

  1. 这样一切都运行得很好。
  2. 现在我将其打印出来。
  3. 我得到了这个:

[[ , 0, 1, 2, 3, 4, 5, 6, 7, ], [0, [], +, [], +, +, +, +, [], ], [1, +, +, +, +, +, +, +, +, ], [2, +, +, +, [], +, +, +, [], ], [3, +, +, +, [], +, +, +, +, ], [4, +, [], +, +, +, +, +, +, ], [5, +, [], +, +, +, +, +, +, ], [6, +, +, +, +, +, +, +, +, ], [7, +, +, +, +, +, +, +, +, ], [ , , , , , , , , , ]]

  1. 我现在尝试使用`.replace`方法来构建我的游乐场,使其看起来像一个游乐场:

System.out.println(Arrays.deepToString(playGround.getPlayGroundArray())
.replace("[[", " ")
.replace("]]", " \n")
.replace("], [", " \n")
.replace("]", " ")
.replace(",", " ")
.replace("[", " "));

  1. 不是我想要的:P

0 1 2 3 4 5 6 7
0 + + + + + *

1 + * + + + + + +
2 * + + + + + + +
3 + * + + + + + +
4 + * + + + + * +
5 + + + + + + + *
6 + + + + + + + +
7 + + * + + + + +

  1. 因此,结果应该看起来像这样:

0 1 2 3 4 5 6 7
0 + * * + + + + +
1 + + + + * + + +
2 + + * + + + + *
3 * + + + + + + +
4 + + * + + * * +
5 + + + + + + + +
6 + + + + + + * +
7 + + + + + + + +

  1. 我最接近的结果是上面的代码。
  2. 也许有其他解决方案,因为我尝试了很多方法,但数组的括号是一样的,所以你无法在不改变字符串数组括号的情况下替换字符数组的括号。
  3. 每隔10个元素,应该换行。
  4. 谢谢你的帮助 :)
英文:

So i got this String 2Dimensional array that prints me out a playGround :

  1. private final int rowX = 10;
  2. private final int colY = 10;
  3. private final String[][] playGroundArray = new String[rowX][colY];

Then i added with a loop to 10 random positions in my playGroundArray, an char array.

  1. mines[0] = &quot;*&quot;;
  2. for (int i = 0; i &lt; numberOfMines; i++) {
  3. getPlayGroundArray()[random.nextInt(playGroundArray.length - 2) + 1][random.nextInt(playGroundArray.length - 2) + 1] = Arrays.deepToString(mines);
  4. }

so that all works fine.

Now i print it out.
I get this:

  1. [[ , 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:

  1. System.out.println(Arrays.deepToString(playGround.getPlayGroundArray())
  2. .replace(&quot;[[&quot;, &quot; &quot;)
  3. .replace(&quot;]]&quot;, &quot; \n&quot;)
  4. .replace(&quot;], [&quot;, &quot; \n&quot;)
  5. .replace(&quot;]&quot;, &quot; &quot;)
  6. .replace(&quot;,&quot;, &quot; &quot;)
  7. .replace(&quot;[&quot;, &quot; &quot;));

Not really that what i want 替换打印语句中的元素

  1. 0 1 2 3 4 5 6 7
  2. 0 + + + + + *
  3. * +
  4. 1 + * + + + + + +
  5. 2 * + + + + + + +
  6. 3 + * + + + + + +
  7. 4 + * + + + + * +
  8. 5 + + + + + + + *
  9. 6 + + + + + + + +
  10. 7 + + * + + + + +

So the result should look like that

  1. 0 1 2 3 4 5 6 7
  2. 0 + * * + + + + +
  3. 1 + + + + * + + +
  4. 2 + + * + + + + *
  5. 3 * + + + + + + +
  6. 4 + + * + + * * +
  7. 5 + + + + + + + +
  8. 6 + + + + + + * +
  9. 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

以下是翻译好的内容:

你不应该使用字符串来表示游乐场 - 使用布尔值会更容易,并且不要在枚举本身中包含坐标:

  1. import java.util.*;
  2. public class Playground {
  3. private boolean mines[][];
  4. private int rows;
  5. private int cols;
  6. public Playground(int rows, int cols, int mineCount) {
  7. this.rows = rows; this.cols = cols;
  8. mines = new boolean[rows][cols];
  9. Random rng = new Random();
  10. while (mineCount > 0) {
  11. int r = rng.nextInt(rows);
  12. int c = rng.nextInt(cols);
  13. if (!hasMine(r, c)) { // 如果已经有地雷,则重新放置
  14. mines[r][c] = true;
  15. mineCount--;
  16. }
  17. }
  18. }
  19. boolean hasMine(int row, int col) {
  20. return mines[row][col];
  21. }
  22. @Override
  23. public String toString() {
  24. StringBuilder sb = new StringBuilder(" "); // 以3个空格开头
  25. for (int c = 0; c < cols; c++) {
  26. sb.append(String.format("%3d", c)); // 使用空格填充数字左侧
  27. }
  28. for (int r = 0; r < rows; r++) {
  29. sb.append(String.format("\n%3d", r));
  30. for (int c = 0; c < cols; c++) {
  31. sb.append(String.format("%3s", (hasMine(r, c) ? "*" : "+")));
  32. }
  33. }
  34. return sb.append("\n").toString();
  35. }
  36. public static void main(String... args) {
  37. System.out.println(new Playground(10, 10, 10));
  38. }
  39. }

正则表达式很棒,实际上你几乎可以使用大量的正则表达式来完成任何事情。这并不意味着它们适用于所有工作。

英文:

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:

  1. import java.util.*;
  2. public class Playground {
  3. private boolean mines[][];
  4. private int rows;
  5. private int cols;
  6. public Playground(int rows, int cols, int mineCount) {
  7. this.rows = rows; this.cols = cols;
  8. mines = new boolean[rows][cols];
  9. Random rng = new Random();
  10. while (mineCount &gt; 0) {
  11. int r = rng.nextInt(rows);
  12. int c = rng.nextInt(cols);
  13. if ( ! hasMine(r, c)) { // retries placement if mine already there
  14. mines[r][c] = true;
  15. mineCount --;
  16. }
  17. }
  18. }
  19. boolean hasMine(int row, int col) {
  20. return mines[row][col];
  21. }
  22. @Override
  23. public String toString() {
  24. StringBuilder sb = new StringBuilder(&quot; &quot;); // starts with 3 spaces
  25. for (int c=0; c&lt;cols; c++) {
  26. sb.append(String.format(&quot;%3d&quot;, c)); // left-pads a number with spaces
  27. }
  28. for (int r=0; r&lt;rows; r++) {
  29. sb.append(String.format(&quot;\n%3d&quot;, r));
  30. for (int c=0; c&lt;cols; c++) {
  31. sb.append(String.format(&quot;%3s&quot;, (hasMine(r, c) ? &quot;*&quot; : &quot;+&quot;)));
  32. }
  33. }
  34. return sb.append(&quot;\n&quot;).toString();
  35. }
  36. public static void main(String ... args) {
  37. System.out.println(new Playground(10, 10, 10));
  38. }
  39. }

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.

huangapple
  • 本文由 发表于 2020年10月13日 18:15:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64333249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定