본문으로 바로가기

참고)

자바(java) 조건문은 https://codehouse.tistory.com/7, https://codehouse.tistory.com/8 에서 확인 가능하다.

자바(java) 반복문은 https://codehouse.tistory.com/11https://codehouse.tistory.com/12 에서 확인 가능하다.

 

 

1. 계단형

 

*

**

***

****

*****

******

*******

********

*********

**********

 

 

코드)

1
2
3
4
5
6
7
for(int i=1; i <= 10; i++) {
    for(int j=1; j <= i; j++ ) {
        System.out.print("*");
    }
 
    System.out.println();
}
 

 

 

 

2. 계단형(역방향)

 

*********

********

*******

******

*****

****

***

**

*

 

 

코드)

1
2
3
4
5
6
7
for(int i=1; i <= 10; i++) {
    for(int j=1; j <=10-i; j++ ) {
        System.out.print("*");
    }
    
    System.out.println();
}
 

 

 

 

3. 피라미드형

 

         *

        ***

       *****

      *******

     *********

    ***********

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

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

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

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

 

 

코드)

1
2
3
4
5
6
7
8
9
for (int i = 0; i < 10; i++) { 
    for (int j = 10-1; j > i; j--) { 
        System.out.print(" "); 
    } 
    for (int k = 0; k < 2*i+1; k++) { 
        System.out.print("*"); 
    } 
        System.out.println(); 
}
 
 

 

 

 

4. 피라미드형(역방향)

 

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

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

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

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

    ***********

     *********

      *******

       *****

        ***

         *

 

코드)

1
2
3
4
5
6
7
8
9
10
11
for(int i=10; i > 0; i--) {
    for(int j=0; j < 10 - i; j++) {
        System.out.print(" ");
    }
 
    for(int k=0; k< 2*i-1; k++) {
        System.out.print("*");
    }
 
    System.out.println();
}
 
 

 

 

 

5. 다이아몬드형

 

  * 

 ***

*****

 ***

  * 

 

 

코드)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
for(int i=0; i < 5; i++) {
    for(int j=0; j < 5; j++) {
        if(i <= 5 / 2) {
            if(i+<= 5/2-1) {
                System.out.print(" ");
            }else if(j->= 5/2+1) {
                System.out.print(" ");
            }else {
                System.out.print("*");
            }
        }else if (i>5/2)
        {
            if (i-j>=5/2+1)
                System.out.print(" ");
            else if (i+j>=5/2*3+1)
                System.out.print(" ");
            else
                System.out.print("*"); 
        }
    }
    System.out.println();
}