728x90
반응형

문제 1

    *
   **
  ***
 ****
*****
  • 반복문을 이용하라.
  • ' '(공백)과 '*'(별)을 이용해 위 그림과 같이 출력하라.

 

 

▶ 코드

		int max=5;
		int row,column;
        
		for(row=0;row<max;row++){
			for(column=0;column<(max-row)-1;column++){
				System.out.print(" ");
			}
			
			for(column=0;column<row+1;column++){
				System.out.print("*");
			}
			System.out.println();//줄바꿈
		}

 

▶ 출력 결과

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

 

 

 

문제 2

     *
    ***
   *****
  *******
 *********
***********
 *********
  *******
   *****
    ***
     *
  • 반복문을 이용하라.
  • ' '(공백)과 '*'(별)을 이용해 위 그림과 같이 출력하라.

 

 

▶ 코드

		int i,j,k;
		int max=5;//5줄을 기준으로 출력하기 위해 미리 선언
        
		//5줄 출력
		for(i=0;i<max;i++){//0~4
			for(j=0;j<max-i;j++){
				System.out.print(" ");
			}
			
			for(k=0;k<i*2+1;k++){
				System.out.print("*");
			}
			System.out.println(); //줄바꿈
		}////out_for1
		
		//6줄 출력
		for(i=max;i>=0;i--)//5~0
		{
			for(j=0;j<max-i;j++){
				System.out.print(" ");
			}
			
			for(k=0;k<i*2+1;k++){
				System.out.print("*");
			}
			System.out.println(); //줄바꿈
		}//out_for2

 

▶ 출력 결과

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

 

 

 

문제 3

***** *****
****   ****
***     ***
**       **
*         *
*         *
**       **
***     ***
****   ****
***** *****
  • 반복문을 이용하라.
  • ' '(공백)과 '*'(별)을 이용해 위 그림과 같이 출력하라.

 

 

▶ 코드

		int max=5;
		int row, column;
		
		//위
		for(row=0;row<max;row++){
			for(column=0;column<max-row;column++){
				System.out.print("*");
			}//inner_for1
			
			for(column=0;column<(row*2)+1;column++){
				System.out.print(" ");
			}//inner_for2
			
			for(column=0;column<max-row;column++){
				System.out.print("*");
			}//inner_for3
	
			System.out.println(); //줄바꿈
		}//out_for1
		
		//아래
		for(row=max-1;row>=0;row--){
			for(column=0;column<max-row;column++){
				System.out.print("*");
			}//inner_for1
			
			for(column=0;column<(row*2)+1;column++){
				System.out.print(" ");
			}//inner_for2
			
			for(column=0;column<max-row;column++){
				System.out.print("*");
			}//inner_for3
			
			System.out.println(); //줄바꿈
		}//out_for2

 

▶ 출력 결과

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

 

 

 

728x90
반응형

+ Recent posts