0

我在创建菱形时遇到问题,我的代码在这里:

package random;

public class asd {
    public static void main(String args[]) {
        for (int j = 1; j <= 4; j++) {
            for (int kong = 4 - j; kong >= 1; kong--) {
                System.out.print(" ");
            }
            for (int xing = 1; xing <= 2 * j - 1; xing++) {
                System.out.print("*");
            }
            System.out.println();
        }
        for (int a = 1; a <= 3; a++) {
            for (int b = 1; b <= a; b++) {
                System.out.print(" ");
            }
            for (int c = 5; c >= 1; c -= 2) { // <==== here
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

但是,输出是:

   *
  ***
 *****
*******
 ***
  ***
   ***

我认为问题出在我突出显示的代码中。

4

4 回答 4

2

java-11

通过使用String#repeat, 作为 Java-11 的一部分引入,您可以使用单个循环来完成。

public class Main {
    public static void main(String[] args) {
        int size = 9;
        int midRowNum = size / 2 + 1;
        for (int i = 1 - midRowNum; i < midRowNum; i++) {
            System.out.println(" ".repeat(Math.abs(i)) + "*".repeat((midRowNum - Math.abs(i)) * 2 - 1));
        }
    }
}

输出:

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

通过将空格量增加一个字符,您还可以打印形状的变体:

public class Main {
    public static void main(String[] args) {
        int size = 9;
        int midRowNum = size / 2 + 1;
        for (int i = 1 - midRowNum; i < midRowNum; i++) {
            System.out.println("  ".repeat(Math.abs(i)) + "* ".repeat((midRowNum - Math.abs(i)) * 2 - 1));
        }
    }
}

输出:

        * 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * * 
  * * * * * * * 
    * * * * * 
      * * * 
        * 
于 2021-01-17T22:37:12.127 回答
0

您指出可能有问题的行是正确的。很惊讶你在上半场做得对:

for (int c = 5; c >= 2 * a - 1; c -= 1) { // <==== here
    System.out.print("*");
于 2013-08-25T15:39:45.787 回答
0

使用Math.abs将使它更容易:

import java.util.Scanner;

public class MakeDiamond {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("Let's Creat Diamonds");
            System.out.println("If number increases Diamonds gets bigger. " +
                    "Please input number lager than 1 : ");

            int user_input = sc.nextInt(); //gets user's input
            System.out.println("");

            int x = user_input;
            int front_space = -5;

            for (int i = 0; i < 2 * user_input + 1; i++) {
                for (int a = front_space; a < Math.abs(i - user_input); a++) {
                    System.out.print("    ");
                }

                if (i < user_input + 1) {
                    for (int b = 0; b < 2 * i + 1; b++) {
                        System.out.print("*  ");
                    }

                } else if (i > user_input) {
                    for (int c = 0; c < 2 * x - 1; c++) {
                        System.out.print("*  ");
                    }
                    x--;
                }
                System.out.print('\n');
            }

            System.out.println("\nRun Again? 1 = Run,  2 = Exit : ");

            int restart = sc.nextInt();
            System.out.println("");

            if (restart == 2) {
                System.out.println("Exit the Program.");
                System.exit(0);
                sc.close();
            }
        }
    }
}
于 2016-07-08T05:53:35.997 回答
0

您可以通过使用两个嵌套的 for 循环和一个if else 语句来简化您的代码。

int n = 4;
for (int i = -n; i <= n; i++) {
    for (int j = -n; j <= n; j++)
        if (Math.abs(i) + Math.abs(j) <= n)
            System.out.print("*");
        else
            System.out.print(" ");
    System.out.println();
}

输出:

    *    
   ***   
  *****  
 ******* 
*********
 ******* 
  *****  
   ***   
    *    

另请参阅:使用循环输出 ASCII 菱形

于 2021-06-17T21:29:20.550 回答