Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
607 views
in Technique[技术] by (71.8m points)

geometry - Make a upside down triangle in java

I am trying to make the triangle I have made up side down. Tried many times, but I don't know how to do this.

The code I have know is:

public static void drawPyramide(int lines, char symbol, boolean startDown) {
    //TRIANGLE

    if(startDown) {
                //The triangle up side down should be here. 
            }

    else {
        int c = 1;
        for (int i = 0; i < lines; i++) {
            for (int j = i; j < lines; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= c; k++) {
                if (k%2==0) System.out.print(" ");

                else System.out.print(symbol);
            }

        System.out.print("
");
        c += 2;
        }
    }

}

Any suggestions how I can "flip" this triangle? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To flip the triangle you really just need to change the direction of iteration. Instead of going from i = 0 to i < lines you need to go down from i = lines-1 to i >= 0

You also need to change the c to how many spaces and symbols you want to start with.

Could look like this:

int c = 2*lines;
for (int i = lines-1; i>=0; i--)
{
    for (int j = i; j < lines; j++)
    {
        System.out.print(" ");
    }
    for (int k = 1; k <= c; k++)
    {
        if (k % 2 == 0)
        {
            System.out.print(" ");
        }
        else
        {
            System.out.print(symbol);
        }
    }

    System.out.print("
");
    c -= 2;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...