简体   繁体   English

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

[英]Print a rectangle with diagonals using a 2d array in Java

I have been trying to modify the normal rectangle 2d array, that is printed to the console, to show the diagonals of it as well with a different character. 我一直在尝试修改打印到控制台的普通矩形2d数组,以显示其对角线以及其他字符。 For example, my current code for a rectangle with a 2d array is: 例如,我当前具有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);
    }
  }
}

and it returns: 它返回:

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

I want my diagonal code to return: 我希望对角线代码返回:

Height: 5
Width: 7
xooooox
oxoooxo
ooxxxoo
oxoooxo
xooooox

My current code is: 我当前的代码是:

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

And this returns: 并返回:

Height: 5
Width: 7
xoooxox
oxoxoxo
ooxoxoo
oxoxooo
xoxooox

Please help me in solving this problem and thanks in advance. 请帮助我解决这个问题,并在此先感谢。

Here is one way to do it, with reusable method separating the various operations applied to the rectangle. 这是一种实现方法,可重用的方法将应用于矩形的各种操作分开。

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

Test 测试

printRectangleWithDiagonals(7, 7);
System.out.println();
printRectangleWithDiagonals(10, 4);
System.out.println();
printRectangleWithDiagonals(5, 9);

Output 产量

xooooox
oxoooxo
ooxoxoo
oooxooo
ooxoxoo
oxoooxo
xooooox

xxooooooxx
ooxxxxxxoo
ooxxxxxxoo
xxooooooxx

xooox
oxoxo
oxoxo
ooxoo
ooxoo
oxoxo
oxoxo
xooox
xooox

As far as i understand it you'd like to display some sort of a cross. 据我了解,您想展示某种十字架。 And you want to handle the case where the matrix is not a square. 您要处理矩阵不是正方形的情况。

That means you could directly go from all corners to the center point and if one axis reaches the middle of the array first just stop the counter and proceed with the second param. 这意味着您可以直接从所有角度到达中心点,如果一个轴到达数组的中间,则首先停止计数器并继续第二个参数。

something like that (just pseudo code): 像这样的东西(只是伪代码):

//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++ }
}

Hope this helps a bit. 希望这个对你有帮助。 In general i would advise splitting up your problem in different parts. 通常,我建议将您的问题分解为不同的部分。

I can see the logic in your solution but try to keep it simple. 我可以在您的解决方案中看到逻辑,但请尝试使其保持简单。 Find rules that work as long as a condition is true. 查找只要条件为真的规则。 (In this case: as long as you are not in the middle of any array) Then try to find a solution for cases that are not true. (在这种情况下:只要您不在任何数组中间),然后尝试为不正确的情况找到解决方案。 (eg what happens if i reaches the middle of the array but j doesnt) (例如,如果我到达数组的中间但j没有,会发生什么)

Like that you can split up your code and make it much easier to read/maintain. 这样,您可以拆分代码,使其更易于阅读/维护。

In most cases where you have huge if else statements there is a big chance you can rewrite them into smaller parts. 在大多数情况下,如果您有大量的if else语句,则很有可能将它们重写为较小的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM