Regarding a star pattern

前端 未结 3 1661
独厮守ぢ
独厮守ぢ 2021-01-29 14:51

I am trying to print below star pattern

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

I am using below logic to print :

*
***
*****

3条回答
  •  迷失自我
    2021-01-29 15:39

    You just have to write in reverse the loop, to start from the upperBound - 1. See the code bellow:

    int numberOfLines = 3;
    for (int i = 1; i <= numberOfLines; i++) {
        for (int j = 1; j < 2*i; j++){
            System.out.print("*");
        }
        System.out.println();
    }
    for (int i = numberOfLines - 1; i > 0; i--) {
        for (int j = 1; j < 2*i; j++){
            System.out.print("*");
        }
        System.out.println();
    }
    

提交回复
热议问题