简体   繁体   English

需要帮助调整Java中的钻石形状

[英]Need help adjusting a diamond shape in Java

I wrote some code that will print a diamond 我写了一些会打印钻石的代码

static void printDiamond(int size) {
    for (int i = 0; i < size; i++) {
        for (int a = 0; a < (size - (i + 1)); a++) {
            System.out.print(" ");
        }
        System.out.print("X");
        for (int b = 0; b < (i * 2); b++) {
            System.out.print(" ");
        }
        System.out.print("X");
        System.out.println();
    }

    for (int i = size-1; i >= 0; i--) {
        for (int a = 0; a < (size - (i + 1)); a++) {
            System.out.print(" ");
        }
        System.out.print("X");
        for (int b = 0; b < (i * 2); b++) {
            System.out.print(" ");
        }
        System.out.print("X");
        System.out.println();
    }
}

The issue I'm having with the diamond is that it will print double for whatever I input. 我对钻石的问题在于,无论我输入什么,它都会打印出来。 So, if the user were to input a 6 for the diamond, it should look like this: 因此,如果用户要为钻石输入6,它应该如下所示:

  XX
 X  X
X    X
X    X
 X  X
  XX

With my code, if the user inputs 5, it prints out the following: 使用我的代码,如果用户输入5,它将打印出以下内容:

    XX
   X  X
  X    X
 X      X
X        X
X        X
 X      X
  X    X
   X  X
    XX

Instead of printing out 5 rows, it printed out 10. If I input 3, it will print out 6 rows instead of 3. It seems that for my diamond, it is outputting the number that it receives from the user, but then prints out that amount times 2. Is there a way I can half the size of what the printDiamond method outputs so it has the correct number of rows? 它打印出10而不是打印5行。如果我输入3,它将打印出6行而不是3行。看来,对于我的钻石,它输出的是从用户收到的数字,但随后打印出来那个数量是2。有没有办法我可以输出printDiamond方法输出的一半,所以它有正确的行数?

I was able to correct your code by tweaking the loop boundary conditions. 我能够通过调整循环边界条件来纠正您的代码。 First, you were printing the top portion of your diamond with a height of size and you were also printing the bottom potion with a height of size , for a total height of 2*size . 首先,您正在打印size高度的钻石顶部 ,并且您打印了高度size的底部部分,总高度为2*size

The other big problem was that you were not handling odd-numbered inputs, as all diamonds were coming out as even number height. 另一个大问题是你没有处理奇数输入,因为所有的钻石都是偶数高度。 I also corrected this problem. 我也纠正了这个问题。 Have a look at the code below. 看看下面的代码。

static void printDiamond(int size) {
    for (int i = 0; i < (int)Math.ceil(size/2.0); i++) {
        for (int a = 0; a < (int)Math.ceil(size/2.0) - (i + 1); a++) {
            System.out.print(" ");
        }
        System.out.print("X");
        for (int b = 0; b < (i * 2); b++) {
            System.out.print(" ");
        }
        System.out.print("X");
        System.out.println();
    }

    for (int i = (int)Math.floor(size/2.0)-1; i >= 0; i--) {
        for (int a = 0; a < (int)Math.ceil(size/2.0) - (i + 1); a++) {
            System.out.print(" ");
        }
        System.out.print("X");
        for (int b = 0; b < (i * 2); b++) {
            System.out.print(" ");
        }
        System.out.print("X");
        System.out.println();
    }
}

printDiamond(5);
System.out.print("\n");
printDiamond(6);

Output: 输出:

  XX
 X  X
X    X
 X  X
  XX

  XX
 X  X
X    X
X    X
 X  X
  XX

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

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