首页 > 解决方案 > 在Java中使用二维数组打印一个对角线的矩形

问题描述

我一直在尝试修改打印到控制台的普通矩形二维数组,以显示它的对角线以及不同的字符。例如,我当前的二维数组矩形代码是:

import java.util.Scanner;

class RecArray {
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Height: ");
    int height = scanner.nextInt();
    System.out.print("Width: ");
    int width = scanner.nextInt();


    char[][] square = new char[height][width];

    String line;

    // fill the array
    for (int i = 0; i < height; i++) {
      for (int j = 0; j < width; j++) {
        square[i][j] = 'o';
      }
    }

    // print the array
    for (int i = 0; i < height; i++) {
      line = "";
      for (int j = 0; j < width; j++) {
        line += square[i][j];
      }
      System.out.println(line);
    }
  }
}

它返回:

Height: 10
Width: 10
oooooooooo
oooooooooo
oooooooooo
oooooooooo
oooooooooo
oooooooooo
oooooooooo
oooooooooo
oooooooooo
oooooooooo

我希望我的对角线代码返回:

Height: 5
Width: 7
xooooox
oxoooxo
ooxxxoo
oxoooxo
xooooox

我目前的代码是:

 import java.util.Scanner;

    class RecArrayDiag {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Height: ");
        int height = scanner.nextInt();
        System.out.print("Width: ");
        int width = scanner.nextInt();

        char[][] square = new char[height][width];
        boolean bool1 = true;
        boolean bool2 = true;
        boolean bool3 = true;
        boolean bool4 = true;
        String line;
        int x = 0;
        for (int i = 0; i < height; i++) {
            for (int j = width-1; j >= 0; j--) {
                if (i % 2 == 0 ? ((i == height/2)) : ((i == height-1/2))) {
                    bool1 = false;
                }
                if (j % 2 == 0 ? ((j == width/2)) : ((j == width-1/2))) {
                    bool2 = false;
                }

                if ((((i == j) && bool1 && bool2) || (i == (height - (j+1))) || (j == (width - (i+1))) || ((j == width-1) && bool3) || ((i == height-1) && bool4) || (j == width-1) && (i == height-1))) {
                    square[i][j] = 'x';
                    //x++;
                } else {
                    square[i][j] = 'o';
                }
                if ((j == width-1)) {
                    bool3 = false;
                }
                if ((i == height-1)) {
                    bool4 = false;
                }
            }
            x++;
        }

        // print the array
        for (int i = 0; i < height; i++) {
            line = "";
            for (int j = 0; j < width; j++) {
                line += square[i][j];
            }
            System.out.println(line);
        }
    }
}

这会返回:

Height: 5
Width: 7
xoooxox
oxoxoxo
ooxoxoo
oxoxooo
xoxooox

请帮助我解决这个问题,并在此先感谢。

标签: javamultidimensional-arrayprintingconsole

解决方案


据我了解,您想展示某种十字架。并且您想要处理矩阵不是正方形的情况。

这意味着您可以直接从所有角落到中心点,如果一个轴首先到达阵列的中间,只需停止计数器并继续执行第二个参数。

类似的东西(只是伪代码):

//create square with "o" everywhere then overwrite
int i = 0;
int j = 0;
while(i < height/2 || j < width/2){

    //go from all corners towards the middle
    if (i == j){
       square[i][j] = "x";
       square[i][width - j+1] = "x";
       square[height - i+1][j] = "x";
       square[height - i+1][width - j+1] = "x";
    } else if (i < height/2) { //i is in middle of array
       square[i][j] = "x";
       square[i][width - j+1] = "x";
    } else { //j is is in middle of array
       square[i][j] = "x";
       square[height - i+1][j] = "x";
    }

    //as long i and j did not reach the center add 1
    if (i < width/2) { i++ }
    if (j < height/2) { j++ }
}

希望这个对你有帮助。一般来说,我建议将您的问题分成不同的部分。

我可以在您的解决方案中看到逻辑,但尽量保持简单。查找只要条件为真就有效的规则。(在这种情况下:只要您不在任何数组的中间)然后尝试为不正确的情况找到解决方案。(例如,如果我到达数组的中间但 j 没有,会发生什么)

像这样,您可以拆分代码并使其更易于阅读/维护。

在大多数情况下,如果您有大量 if else 语句,您很有可能可以将它们重写为更小的部分。


推荐阅读