Explain creating a pyramid in Java

后端 未结 7 1265
独厮守ぢ
独厮守ぢ 2020-12-18 17:04

I am in a beginner java programming class and currently reviewing loops. If there is one thing I just do not understand, it\'s pyramids. I have researched the book exercis

7条回答
  •  有刺的猬
    2020-12-18 18:02

    There are 7 rows in the pyramid, so the first for loop is looping over the rows, the second for loop is printing a bunch of spaces so that the triangle doesnt appear as:

    1
    2 1 2
    3 2 1 2 3
    

    The third for loop (with k), has a conditional operator which works like this:

    boolean-expression ? result-if-true : result-if-false
    

    So it either adds the number k to the string, or adds a space and then the number k to the string.

    Fourth loop does a similar thing.

提交回复
热议问题