*을 이용해서 트리를 만드는 방법 2가지와 뒤집은 트리를 만드는 방법 2가지
결과물은 아래와 같다.

합치면 다이아몬드도 만들 수 있다!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package togit; | |
public class MakeTree { | |
public static void main(String[] args) { | |
int line = 5; //이 부분의 숫자를 바꾸면 트리의 크기가 달라진다 | |
System.out.println("방법1\n"); | |
String tree = "*"; | |
for (int i = 0; i < line; i++) { | |
for (int j = (line - 1) - i; j > 0; j--) { | |
System.out.print(" "); | |
} | |
System.out.println(tree); | |
tree += "**"; | |
} | |
System.out.println("\n방법2\n"); | |
for (int i = 0 ; i < line; i++) { | |
for(int j = (line-1)-i ; j > 0 ; j--) { | |
System.out.print(" "); | |
} | |
for(int k =(i*2)+1 ; k>0 ; k--) { | |
System.out.print("*"); | |
} | |
System.out.println(); | |
} | |
System.out.println("\n트리를 뒤집어보자"); | |
System.out.println("방법1\n"); | |
String blank = ""; | |
for(int i = 0 ; i < line; i++) { | |
String star = "*"; | |
for(int j = 0 ; j < (line-1)-i ; j++) { | |
star += "**"; | |
} | |
System.out.println(blank + star); | |
blank += " "; | |
} | |
System.out.println("\n방법2\n"); | |
tree = "*********"; | |
for (int i = 0; i < 5 ; i++) { | |
for (int j = i; j > 0; j--) { | |
System.out.print(" "); | |
} | |
System.out.println(tree.substring(i,tree.length()-i)); | |
} | |
} | |
} |
'JAVA' 카테고리의 다른 글
Java - 삼항 연산자로 주민등록번호를 분석해 성별 알아내기 (0) | 2022.05.17 |
---|---|
JAVA - Scanner로 성적을 받아서 평균 계산하기 (0) | 2022.05.11 |
Java - HashMap을 이용한 아이디, 비밀번호 체크 (0) | 2022.04.30 |
Java 변수의 선언과 값 바꾸기 (0) | 2022.04.28 |
Java의 기본타입과 변수 선언 (0) | 2022.04.28 |
댓글