替换打印语句中的元素

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

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] = &quot;*&quot;;
for (int i = 0; i &lt; 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(&quot;[[&quot;, &quot; &quot;)
                .replace(&quot;]]&quot;, &quot; \n&quot;)
                .replace(&quot;], [&quot;, &quot; \n&quot;)
                .replace(&quot;]&quot;, &quot; &quot;)
                .replace(&quot;,&quot;, &quot; &quot;)
                .replace(&quot;[&quot;, &quot; &quot;));

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 &gt; 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(&quot;   &quot;); // starts with 3 spaces
for (int c=0; c&lt;cols; c++) {
sb.append(String.format(&quot;%3d&quot;, c));      // left-pads a number with spaces
}
for (int r=0; r&lt;rows; r++) {
sb.append(String.format(&quot;\n%3d&quot;, r));
for (int c=0; c&lt;cols; c++) {
sb.append(String.format(&quot;%3s&quot;, (hasMine(r, c) ? &quot;*&quot; : &quot;+&quot;)));
}
}
return sb.append(&quot;\n&quot;).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.

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:

确定