使用Java中的二维数组打印带有对角线的矩形

使用Java中的二维数组打印带有对角线的矩形,java,multidimensional-array,printing,console,Java,Multidimensional Array,Printing,Console,我一直在尝试修改打印到控制台的普通矩形2d数组,以便用不同的字符显示它的对角线。例如,我当前的二维数组矩形代码是: 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.ne

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

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

请帮助我解决这个问题,并提前表示感谢。

这里有一种方法可以做到这一点,使用可重用的方法分离应用于矩形的各种操作

public static void printRectangleWithDiagonals(int width, int height) {
    char[][] rectangle = new char[height][width];
    fill(rectangle, 'o');
    drawDiagonals(rectangle, 'x');
    print(rectangle);
}
private static void fill(char[][] rectangle, char ch) {
    for (char[] line : rectangle)
        for (int i = 0; i < line.length; i++)
            line[i] = ch;
}
private static void drawDiagonals(char[][] rectangle, char ch) {
    int bottom = rectangle.length - 1, right = rectangle[0].length - 1;
    if (right > bottom) {
        for (int x = 0; x <= right; x++) {
            int y = (x * bottom + right / 2) / right;
            rectangle[y][x] = ch;
            rectangle[bottom - y][x] = ch;
        }
    } else {
        for (int y = 0; y <= bottom; y++) {
            int x = (y * right + bottom / 2) / bottom;
            rectangle[y][x] = ch;
            rectangle[y][right - x] = ch;
        }
    }
}
private static void print(char[][] rectangle) {
    for (char[] line : rectangle)
        System.out.println(line);
}
输出

xoox
oxoooxo
ooxoxoo
oooxooo
ooxoxoo
oxoooxo
xoooox
xxooooxx
OOXXXXXOO
OOXXXXXOO
xxooooxx
Xoox
氧代
氧代
奥克索
奥克索
氧代
氧代
Xoox
Xoox

这里有一种方法,使用可重用的方法分离应用于矩形的各种操作

public static void printRectangleWithDiagonals(int width, int height) {
    char[][] rectangle = new char[height][width];
    fill(rectangle, 'o');
    drawDiagonals(rectangle, 'x');
    print(rectangle);
}
private static void fill(char[][] rectangle, char ch) {
    for (char[] line : rectangle)
        for (int i = 0; i < line.length; i++)
            line[i] = ch;
}
private static void drawDiagonals(char[][] rectangle, char ch) {
    int bottom = rectangle.length - 1, right = rectangle[0].length - 1;
    if (right > bottom) {
        for (int x = 0; x <= right; x++) {
            int y = (x * bottom + right / 2) / right;
            rectangle[y][x] = ch;
            rectangle[bottom - y][x] = ch;
        }
    } else {
        for (int y = 0; y <= bottom; y++) {
            int x = (y * right + bottom / 2) / bottom;
            rectangle[y][x] = ch;
            rectangle[y][right - x] = ch;
        }
    }
}
private static void print(char[][] rectangle) {
    for (char[] line : rectangle)
        System.out.println(line);
}
输出

xoox
oxoooxo
ooxoxoo
oooxooo
ooxoxoo
oxoooxo
xoooox
xxooooxx
OOXXXXXOO
OOXXXXXOO
xxooooxx
Xoox
氧代
氧代
奥克索
奥克索
氧代
氧代
Xoox
Xoox

据我所知,您希望显示某种十字架。 你想处理矩阵不是正方形的情况

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

类似于此(只是伪代码):

//创建到处都是“o”的正方形,然后覆盖
int i=0;
int j=0;
而(i<高度/2 | | j<宽度/2){
//从各个角落往中间走
如果(i==j){
正方形[i][j]=“x”;
正方形[i][width-j+1]=“x”;
正方形[高度-i+1][j]=“x”;
正方形[高度-i+1][宽度-j+1]=“x”;
}否则,如果(i
希望这有点帮助。 总的来说,我建议把你的问题分成不同的部分

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

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


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

据我所知,您希望显示某种十字。 你想处理矩阵不是正方形的情况

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

类似于此(只是伪代码):

//创建到处都是“o”的正方形,然后覆盖
int i=0;
int j=0;
而(i<高度/2 | | j<宽度/2){
//从各个角落往中间走
如果(i==j){
正方形[i][j]=“x”;
正方形[i][width-j+1]=“x”;
正方形[高度-i+1][j]=“x”;
正方形[高度-i+1][宽度-j+1]=“x”;
}否则,如果(i
希望这有点帮助。 总的来说,我建议把你的问题分成不同的部分

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

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


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

无关提示:要打印,可以对(char[]line:square){System.out.println(line);}
执行
操作,该操作使用方法。仅供参考:a具有相同的宽度和高度。当宽度和高度可能不同时,它被称为a。您的
square
变量命名错误。int 1/2总是0Oh,我明白了。更糟糕的是:你应该把括号放在那里:(宽度-1)/2很难理解。请解释你的逻辑来找出对角线。所有这些bool,请解释。无关提示:要打印,您可以对使用该方法的(char[]line:square){System.out.println(line);}
。仅供参考:A具有相同的宽度和高度。当宽度和高度可能不同时,它被称为a。您的
square
变量命名错误。int 1/2总是0Oh,我明白了。更糟糕的是:你应该把括号放在那里:(宽度-1)/2很难理解。请解释你的逻辑来找出对角线。请解释一下,所有这些嘘声。
public static void printRectangleWithDiagonals(int width, int height) {
    char[][] rectangle = new char[height][width];
    fill(rectangle, 'o');
    drawDiagonals(rectangle, 'x');
    print(rectangle);
}
private static void fill(char[][] rectangle, char ch) {
    for (char[] line : rectangle)
        for (int i = 0; i < line.length; i++)
            line[i] = ch;
}
private static void drawDiagonals(char[][] rectangle, char ch) {
    int bottom = rectangle.length - 1, right = rectangle[0].length - 1;
    if (right > bottom) {
        for (int x = 0; x <= right; x++) {
            int y = (x * bottom + right / 2) / right;
            rectangle[y][x] = ch;
            rectangle[bottom - y][x] = ch;
        }
    } else {
        for (int y = 0; y <= bottom; y++) {
            int x = (y * right + bottom / 2) / bottom;
            rectangle[y][x] = ch;
            rectangle[y][right - x] = ch;
        }
    }
}
private static void print(char[][] rectangle) {
    for (char[] line : rectangle)
        System.out.println(line);
}
printRectangleWithDiagonals(7, 7);
System.out.println();
printRectangleWithDiagonals(10, 4);
System.out.println();
printRectangleWithDiagonals(5, 9);
//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++ }
}