Diamond outline pattern java using loops

前端 未结 2 1129
终归单人心
终归单人心 2021-01-29 07:25

I\'m a novice in java and I\'m a little bit of trouble trying to get the output I need. I\'m trying to make a Diamond outline pattern using nested loops in java. The output I ne

2条回答
  •  灰色年华
    2021-01-29 08:04

    You were close enough, just some small changes

    public static void drawDiamond(int n) {
        int spaces = n - 1;
        for (int i = 0; i < 2 * n - 1; i++) {
            for (int j = 0; j < spaces; j++)
                System.out.print(" ");
            System.out.print(n - spaces);
            for (int j = 0; j < (2 * n - 2 * spaces - 3); j++)
                System.out.print(" ");
            if (i != 0 && i != 2 * n - 2)
                System.out.print(n - spaces);
            if (i < ((2 * n) - 1) / 2)
                spaces--;
            else
                spaces++;
            System.out.println();
        }
    }
    

提交回复
热议问题