向数组添加边框



我有一个数组,打印时打印如下:

    W V E R T I C A L L 
    R O O A F F L S A B 
    A C R I L I A T O A 
    N D O D K O N W D C 
    D R K E S O O D D K 
    O E E P Z E G L I W 
    M S I I H O A E R A 
    A L R K R R I R E R 
    K O D I D E D R C D 
    H E L W S L E U T H

我怎样才能让它像这样打印:

    # # # # # # # # # # # #
    # W V E R T I C A L L #
    # R O O A F F L S A B #
    # A C R I L I A T O A #
    # N D O D K O N W D C #
    # D R K E S O O D D K #
    # O E E P Z E G L I W #
    # M S I I H O A E R A #
    # A L R K R R I R E R #
    # K O D I D E D R C D #
    # H E L W S L E U T H #
    # # # # # # # # # # # #

我尝试将大小变大以适应额外的字符,但我不断得到异常。我基本上是试图让一个非字母字符在我的数组周围做一个边框,这样当我实现我的填字游戏求解器时,当在周围的字母中搜索一个字母时,它不会到达边界,而是击中非字母字符,然后传递 false。

这是我构建数组的代码:

    for (int i=1; i<row; i++){
        String getChar = (new String(sc.next()));
        for(int j = 1;j<col; j++){
            puzzle[i][j] = getChar.charAt(j);
            }
        }

任何帮助不胜感激!

编辑:这打印了我的数组:

     for (int i=0; i<puzzle.length; i++){
           System.out.print(Arrays.toString(puzzle[i]));
           System.out.println("");
     }

编辑2:这是我的检查方法:

    private static boolean checkNE(int row, int col, String word, char[][] puzzle) {
    //Checking diagonals direction
    for(int letter = 1; letter < word.length(); letter++) {
        if(puzzle[row - letter][col + letter] != word.charAt(letter)) {
            return false;
        }
    }
    return true;
}

不要做边框,只需检查您要检查的位置是否在数组之外,如果是,那么您就知道您不会找到整个单词,因为我们必须在数组之外。

例如,你的数组是n x n,如果你试图访问位置n那么它将是越界的,或者如果你试图访问小于0的位置,它也将是越界的。因此,只需确保访问时它在此范围内即可。

您可能应该使用类或边界检查,但这是您想要执行的操作方法。 假设您的填字游戏是row行和col列,您需要拼图的类型为 char[row+2][col+2] .

String getChar = '';
for (int i=0; i<row+2; i++){
    if (i > 0 && i < row +1) {
       getChar = (new String(sc.next()));
    }
    for(int j = 0;j<col+2; j++){
        if (i % (row + 1) == 0 || i % (col + 1) == 0) {
            puzzle[i][j] = '#';
        else {
            puzzle[i][j] = getChar.charAt(j-1);
        }
    }
}
如果您想

在 ideone.com 上查看它的实际效果,请查看它。

您询问了使用 rowcol 预递增来执行此操作,它看起来像这样:

for (int i=0; i<row; i++){
    if (i > 0 && i < row +1) {
       getChar = (new String(sc.next()));
    }
    for(int j = 0;j<col; j++){
        if (i % (row - 1) == 0 || j % (col - 1) == 0) {
            puzzle2[i][j] = '#';
        } else {
            puzzle2[i][j] = getChar.charAt(j-1);
        }
    }
}
for (int i=1; i<row; i++){
    String getChar = (new String(sc.next()));
    for(int j = 1;j<col; j++){
        puzzle[i][j] = getChar.charAt(j);
    }
}

顺便说一句:请记住,Java 中的索引从 0(而不是 1)开始。在您的示例中,您从 1 迭代到最大列长度,这就是为什么您可以得到IndexOutOfBoundsException

我不是 Java 程序员,但我认为 Java 数组索引从零开始,就像 C 一样。 因此,如果您将拼图设为 10x10,然后尝试访问拼图[1][10],您将收到"异常:越界"错误。 也就是说,谜题不会从谜题[1][1]变成谜题[10][10]。 它从谜题[0][0]到谜题[9][9]。如果这是不正确的,Java程序员请纠正。

您需要使用嵌套循环。首先,您不会在当前代码中打印任何内容,例如数组索引的开头为 1,这样您永远不会在索引为 0 的第一个元素上放置任何内容。

public class BorderArray {
  public static void main(String[] args) {
    // Setup the puzzle, this may change or use input.
    //Assert that this string is 100 size length or row/col will not work.
    String chars = "WVERTICALLROOAFFLSABACRILIATOANDODKONWDCDRKESOODDKOEEPZEGLIWMSIIHOAERAALRKRRIRERKODIDEDRCDHELWSLEUTH";
    int row = 10, col = 10;
    char puzzle[][] = new char[row][col];
    for (int i = 0; i < row; i++) {
      for (int j = 0; j < col; j++) {
        puzzle[i][j] = chars.charAt(i + j);
      }
    }
    // You print the top border.
    // # # # # # # # # # # # # ... print '#' col+2 times
    for (int j = 0; j <= col; j++) {
      System.out.print("# "); // inline
    }
    System.out.println("# "); // move to next line
    // # W V E R T I C A L L # ... print '#' before and after the characters of the row
    // start i at 0 since arrays start at 0
    for (int i = 0; i < row; i++) {
      // for every row print chars of every column inline
      System.out.print("# "); // before
      for (int j = 0; j < col; j++) {
        System.out.print(puzzle[i][j] + " "); // print every char
      }
      System.out.println("#"); // after and next line
    }
    // Bottom border
    // # # # # # # # # # # # # ... print '#' col+2 times, you don't need new line since this is the end.
    // Just use col+1 as limit of the loop.
    for (int j = 0; j <= col + 1; j++) {
      System.out.print("# ");
    }
  }
}

很简单,不要将其构建到数组中。 无论如何,它不是您的实际数据,而是您的"表示层"。

private void printFullBorder(int col) { 
    for (int x=0; x<col+2; x++) {  //+2 because there are the leftmost + rightmost to account for
        System.out.print("#");  //unclear if this should be "#" or " #" based on your formatting, but whatever.
    }
}
printFullBorder(col);
for (int i=0; i<puzzle.length; i++){
    System.out.print("#" + Arrays.toString(puzzle[i]) + "#");
    System.out.println("");
}
printFullBorder(col);
public static String[] addBorder(String picture[]) {
    String[] border = new String[picture.length + 2];
    String pattern = "";
    for (int i = 0; i <= picture[0].length() + 1; i++) {
        pattern = pattern.concat("#");
    }
    border[0] = pattern;
    border[border.length - 1] = pattern;
    for (int i = 1; i <= border.length - 2; i++) {
        border[i] = "#" + picture[i - 1] + "#";
    }
    return border;
}

最新更新