首页 > 解决方案 > 替换打印语句中的元素

问题描述

所以我得到了这个 String 2Dimensional 数组,它打印出一个 playGround :

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 方法来构建我的 playGround,使它看起来像一个 playGround:

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 个元素之后,它应该换行。

谢谢你的帮助 :)

标签: javaarraysreplace

解决方案


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

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));
     }
}

正则表达式很棒,实际上你几乎 可以用大量的正则表达式做任何事情。这并不意味着它们是所有工作的好工具。


推荐阅读