728x90
반응형
<목차>
1) 반복문
1-1) 예제 풀이
2) eclipse 추가 실습 코드
1) 반복문
1-1) 예제 풀이
패키지 Loop_Example05
Loop_Example05.class
package chapter05.loop;
public class Loop_Example05 {
public static void main(String[] args) {
/*
1번 2번
1 0 0 0 0 1 2 3 4 5
0 2 0 0 0 2 3 4 5 6
0 0 3 0 0 3 4 5 6 7
0 0 0 4 0 4 5 6 7 8
0 0 0 0 5 5 6 7 8 9
*/
// 1번 문제
System.out.println("1번");
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
if (i == j) {
System.out.print(i + " ");
} else {
System.out.print("0 ");
}
}
System.out.println();
}
System.out.println("===============");
// 2번 문제
System.out.println("2번");
// 2번 문제 : 내 version
for (int i = 1; i <= 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print((i * 1) + j + " ");
}
System.out.println();
}
System.out.println("===============");
// 2번 문제 : 교수님 version
System.out.println("2번 교수님 버전");
for (int i = 1; i <= 5; i++) {
for (int j = i; j < i + 5; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Loop_Example06.class
package chapter05.loop;
public class Loop_Example06 {
public static void main(String[] args) {
/*
1번 2번
* *****
** ****
*** ***
**** **
***** *
*/
// 1번 문제
System.out.println("1번");
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
// 2번 문제
System.out.println("2번");
for (int i = 5; i > 0; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
// 3번 문제 : 1번, 2번 문제 한 번에 출력
System.out.println("3번");
for (int i = 1; i <= 5; i++) {
// 1번 결과 출력
for (int j = 1; j <= 5; j++) {
if (i >= j) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
// 1번과 2번 결과 사이에 여백 출력
System.out.print("\t");
// 2번 결과 출력
for (int k = 5; k > 0; k--) {
if (k >= i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Loop_Example07.class
package chapter05.loop;
public class Loop_Example07 {
public static void main(String[] args) {
/*
3번 4번
* *****
** ****
*** ***
**** **
***** *
*/
// 3번 문제
System.out.println("3번");
for (int i = 0; i < 5; i++) {
for (int j = 4; j > i; j--) {
System.out.print(" ");
}
for (int k = 0; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}
System.out.println("===========");
// 교수님 version
System.out.println("3번 교수님 버전");
for (int i = 4; i >= 0; i--) {
for (int j = 0; j < 5; j++) {
if (j < i) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println();
}
System.out.println("===========");
// 4번 문제
System.out.println("4번");
for (int i = 5; i > 0; i--) {
for (int j = 1; j <= 5 - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}
System.out.println("===========");
// 교수님 version
System.out.println("4번 교수님 버전");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j < i) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println();
}
}
}
Loop_Example08.class
package chapter05.loop;
public class Loop_Example08 {
public static void main(String[] args) {
/*
1번
*
***
*****
*******
*********
2번
*********
*******
*****
***
*
*/
// 1번
System.out.println("1번");
// 내 version
for (int i = 1; i <= 5; i++) {
for (int j = 5; j > 0; j--) {
if (j > i) {
System.out.print(" ");
} else if (j <= i) {
System.out.print("*");
}
}
for (int k = 1; k < 10; k++) {
if (k <= 5) {
continue;
} else if (k > 5 && k < 5 + i) {
System.out.print("*");
}
}
System.out.println();
}
System.out.println("===============");
// 교수님 version
System.out.println("1번 교수님 버전");
for (int i = 1; i <= 5; i++) {
for (int j = 0; j < 5-i; j++) {
System.out.print(" ");
}
for (int k = 0; k < 2*i-1; k++) {
System.out.print("*");
}
System.out.println();
}
System.out.println("===============");
// 2번
System.out.println("2번");
for (int i = 5; i >= 1; i--) {
for (int j = 5; j > 0; j--) {
if (j > i) {
System.out.print(" ");
} else if (j <= i) {
System.out.print("*");
}
}
for (int k = 1; k < 10; k++) {
if (k <= 5) {
continue;
} else if (k > 5 && k < 5 + i) {
System.out.print("*");
}
}
System.out.println();
}
System.out.println("===============");
// 교수님 version
System.out.println("2번 교수님 버전");
for (int i = 5; i >= 1; i--) {
for (int j = 5-i; j > 0; j--) {
System.out.print(" ");
}
for (int k = 2*i-1; k > 0; k--) {
System.out.print("*");
}
System.out.println();
}
}
}
2) eclipse 추가 실습 코드
package practice;
import java.util.Scanner;
public class ForPractice {
public static void main(String[] args) {
// 연습용 문제1
/*
0
000
00000
*/
Scanner scan = new Scanner(System.in);
System.out.print("Layer 입력: ");
int layer = scan.nextInt();
/*
// version 1
for (int i = 1; i <= layer; i++) {
for (int j = 1; j < layer + i; j++) {
if (j <= layer - i) {
System.out.print(" ");
continue;
}
System.out.print("0");
}
System.out.println();
}
*/
/*
// version 2
for (int i = 0; i < layer; i++) {
// 각 층마다 " "을 찍어주는 반복문
for (int j = layer - (i + 1); j > 0; j--) {
System.out.print(" ");
}
// 각 층마다 "0"을 찍어주는 반복문
for (int k = layer - i; k <= layer + (i * 1); k++) {
System.out.print("0");
}
System.out.println();
}
*/
// version 3
for (int i = 1; i <= layer; i++) {
// 각 층마다 왼쪽 절반을 찍어주는 반복문
for (int j = 1; j <= layer; j++) {
if (j > layer - i) {
System.out.print("0");
} else {
System.out.print(" ");
}
}
// 각 층마다 나머지 오른쪽 절반을 찍어주는 반복문
for (int k = 1; k < layer * 2; k++) {
if (k <= layer) {
continue;
} else if (k >= layer + (i * 1)) {
System.out.print(" ");
} else {
System.out.print("0");
}
}
System.out.println();
}
}
}
'Java' 카테고리의 다른 글
Java(8) - 배열, foreach 문, 메서드, 로또 생성기 (0) | 2024.04.19 |
---|---|
Java(7) - 배열(Array), 다차원 배열 (0) | 2024.04.18 |
Java(5) - 반복문(while 문, do-while 문, for 문) (0) | 2024.04.16 |
Java(4) - 조건문(switch-case 문), 반복문(for 문) (0) | 2024.04.15 |
Java(3) - 제어문(if 조건문), eclipse 실습 코드 (0) | 2024.04.12 |