Javaで*のピラミッドを印刷

補題:

あなたが私を助けることができれば、私は思ったんだけど。私のようなことを数ピラミッドの三角形を表示するJavaでのforループのネストされたルックスを書くしようとしています

___________*#
__________*_*#
_________*___*#
________*_____*#
_______*_______*#
______*_________*#
_____*___________*#
____*_____________*#
___*_______________*#
__*_________________*#
_*___________________*#
***********************#

これは私がこれまで持っているものです。

class Triagle {
    public static void printTriagle(int n) {
        for (int i = 0; i < n; i++) {
            for (int j = n - i; j > 1; j--) {
                System.out.print(" ");
            }

            for (int j = 0; j <= i; j++) {
                // printing stars
                System.out.print("* ");
            }

            System.out.println();
        }
    }

    public static void main(String[] args) {
        printTriagle(12);//I want to set the triangle to be height of 12
    }
 }

私の結果は、予想される出力と等しくないです。

___________*#
__________*_*#
_________*_*_*#
________*_*_*_*#
_______*_*_*_*_*#
______*_*_*_*_*_*#
_____*_*_*_*_*_*_*#
____*_*_*_*_*_*_*_*#
___*_*_*_*_*_*_*_*_*#
__*_*_*_*_*_*_*_*_*_*#
_*_*_*_*_*_*_*_*_*_*_*#
*_*_*_*_*_*_*_*_*_*_*_*#
マーク:

あなたが理解できるように、私はあなたのコードと追加されたコメントを更新しました。以下のコードを参照してください。

public static void printTriagle(int n) {
    for (int i = 0; i < n; i++) {

        for (int j = n - i; j > 1; j--) {
            System.out.print("_");
        }
        String s = "_";
        if (i + 1 >= n) // check if it is the last line
            s = "*"; // change to print * instead of _

        for (int j = 0; j <= i; j++) {
            // printing stars
            if (j == i)
                System.out.print("*#"); // check if the last print of the line
            else if (j == 0)
                System.out.print("*" + s); // check if the first print of the line
            else
                System.out.print(s + s);
        }

        System.out.println();
    }
}

結果:

___________*#
__________*_*#
_________*___*#
________*_____*#
_______*_______*#
______*_________*#
_____*___________*#
____*_____________*#
___*_______________*#
__*_________________*#
_*___________________*#
***********************#

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=186451&siteId=1