하트(HEART) 모양의 ^ ^ 상단 출력


 JAVA로 하트 모양의 ^^ 상단 출력하는 방법을 우선 적어보도록 하겠습니다.

 

int i , j;
int top = 10;
int topWidth = (top / 2) -2;

for( i = topWidth ; i < top; i += 2 ) { 
	for( j = 0; j <= (top - i); j++ ) {
		System.out.print(" ");
	}
			
	for( j = 0; j <= i * 2; j++ ) {
		System.out.print("*");
	}
			
	for( j = 0; j <= (top - i) * 2; j++ ) {
		System.out.print(" ");
	}
			
	for( j = 0; j <= i * 2; j++ ) {
		System.out.print("*");
	}
	
	System.out.print("\n"); 
}

 

 

top은 *의 개수입니다. ( 개수가 많아지면 옆으로 커집니다. )

 

* topWidth 설명.

for( i = topWidth ; i < top; i += 2 )

[ i = 3; i < 10; i += 2 ]

3개부터 2개씩 먼저 더하면서 출력한다는 의미입니다.

하트 모양의 ^^ 상단

System.out.print(" "); 구문이 2개 있는 걸 보실 수 있습니다.

(" ") 안에 1, 2를 추가해서 출력하게 되면 쉽게 이해하실 수 있습니다.

 

하트 모양의 ^^ 상단2

(" ")를 통해 * 간의 간격을 띄워서 모양을 맞추는 것입니다.

 


 

하트(HEART) 모양의 V 하단 출력


다음은 하트 모양의 V 하단 출력하는 방법을 적어보도록 하겠습니다.

 

int i , j;
int top = 10;
int bottom = top * 2 + 1;
		
for( i = bottom; i >= 0; i -= 2 ){ 
	for( j = 0;  j <= bottom - i; j++ ){ 
		System.out.print(" ");
	}
		
	for( j = 0; j <= i * 2; j++ ) { 
		System.out.print("*");
	}
	
	System.out.print("\n");
}

 

bottom은 top의 * 개수에 맞춰서 출력합니다.

 

하트 모양의 V 하단

그리고, 여기서 또한 (" ")은 띄어쓰기를 통해 별 모양을 잡아주는 겁니다.

 

처음엔 한 칸, 다음은 세 칸, 다섯 칸... 계속 띄어쓰기를 넣어줍니다.

 

하트 모양의 V 하단2

 


 

하트(HEART) 모양 완전체 출력

 


int i , j;
int top = 10;
int topWidth = (top / 2) -2;
int bottom = top * 2 + 1;
		 
for( i = topWidth ; i < top; i += 2 ) { 
	for( j = 0; j <= (top - i); j++ ) {
		System.out.print(" ");
	}
	
	for( j = 0; j <= i * 2; j++ ) {
		System.out.print("*");
	}
			
	for( j = 0; j <= (top - i) * 2; j++ ) {
		System.out.print(" ");
	}
	
	for( j = 0; j <= i * 2; j++ ) {
		System.out.print("*");
	}
			
	System.out.print("\n"); 
}
		
for( i = bottom; i >= 0; i -= 2 ){ 
	for( j = 0;  j <= bottom - i; j++ ){ 
		System.out.print(" ");
	}
			
	for( j = 0; j <= i * 2; j++ ) { 
		System.out.print("*");
	}
			
	System.out.print("\n");
}

 

하트모양 완전체

 

하트 모양이 완성되었습니다.

+ Recent posts