본문 바로가기
Programing/JAVA (& 혼공자Java)

자바(Java) - ★찍기2

by a.k.a DUKI 2020. 9. 19.
728x90
반응형

저번 포스팅에 이어, ★찍기를 이용해서 반복문 연습을 하려한다.

 

 

https://dustink.tistory.com/29

 

자바(Java) - ★찍기1

책을 보거나, 구글링을 하다보면, 반복문 연습에는 ★찍기 연습이 제일 좋은 것같다. 그 만큼 예제도 많고, 연습하기 좋은 것같다. ★찍기 연습을 하면 기본적으로 반복되는 코드를 간결하게 줄�

dustink.tistory.com

 

 

ex6) 이렇게 나오려면?

 

★ 

for (int i=0; i<5; i++) { //5단
	for (int j=0; j<5-i; j++) { //5개부터 차례대로 가감
		System.out.print("★");
	}
	System.out.println(); //단 끝날때 줄바꿈
}

 

ex7) 이렇게 나오려면? (10x10)

 

for (int i=0; i<10; i++) { //10열
	for (int j=0; j<10; j++) { //10행
		System.out.print("★");
	}
	System.out.println(); //단 끝날때 줄바꿈
}

 

ex8) 이렇게 나오려면? (10x10)

for (int i = 0; i < 10; i++) { //10열
	for (int j = 0; j < 10; j++) { //10행
		if (i == j) { //i와 j가 같을때만 ★
			System.out.print("★");
		} else { //기본 별은 ☆
			System.out.print("☆");
		}

	}
	System.out.println(); // 단 끝날때 줄바꿈
}

 

ex9) 이렇게 나오려면? (10x10)

0★

1☆

2☆

3☆

4☆

5☆

6☆

7☆

8☆

9☆

for (int i = 0; i < 10; i++) {
	System.out.print(i); //맨앞 숫자 0~9
	for (int j = 0; j < 10; j++) {
		if (i == j) { //i와 j가 같을때 ☆
			System.out.print("☆");
		} else { //나머진 ★
			System.out.print("★");
		}
	}
	System.out.println(); // 단 끝날때 줄바꿈
}

 

 

ex10) 이렇게 나오려면? (10x10)

0☆☆☆☆☆☆☆☆☆☆★
1☆☆☆☆☆☆☆☆☆★★
2☆☆☆☆☆☆☆☆★★★
3☆☆☆☆☆☆☆★★★★
4☆☆☆☆☆☆★★★★★
5☆☆☆☆☆★★★★★★
6☆☆☆☆★★★★★★★
7☆☆☆★★★★★★★★
8☆☆★★★★★★★★★
9☆★★★★★★★★★★

for (int i = 0; i < 10; i++) {
	System.out.printf("%d", i); // printf출력 맨앞 숫자 0~9
	for (int j = 0; j < 10 - i; j++) { //10개에서 i만큼 감소
		System.out.print("☆");
	}
	for (int j = 0; j < i + 1; j++) { //10개에서 i+1만큼 증가
	System.out.print("★");
	}
	System.out.println(); // 단 끝날때 줄바꿈
}

 

 

ex) 이렇게 나오려면? (10x10)

 

 

고민해보길 바란다. 

나중에 봤을때도 바로 기억났으면 좋겠다.

728x90
반응형

댓글