도전 과제 (이중 for 구문)

도전 과제 (이중 for 구문)

💡 학습 목표
    1. 이중 for 구문 사용하기

1. 이중 for 구문을 활용해서 출력 하시오

0   1   2 
0   1   2 
0   1   2
package basic.exercise;

public class ForForMatrix {

	public static void main(String[] args) {

		for (int row = 0; row < 3; row++) {
			for (int col = 0; col < 3; col++) {
				System.out.print(col + "\t");
			}
			System.out.println();
		}
	} // end of main

} // end of class

2. 별표 찍기 (이중 for 구문을 활용한 코드를 작성해주세요)

*
**
***
****
package basic.exercise;

public class Stars1 {

	public static void main(String[] args) {

		// 이중 for구문을 활용해 별표 찍기
//		System.out.println("*");
//		System.out.print("*");
//		System.out.println("*");
//		System.out.print("*");
//		System.out.print("*");
//		System.out.println("*");
//		System.out.print("*");
//		System.out.print("*");
//		System.out.print("*");
//		System.out.println("*");
		
		for (int i = 0; i < 4; i++) {
			for (int l = 0; l < i; l++) {
				System.out.print("*");
			}
			System.out.println("*");
		}
	}

}

풀이

package basic.exercise;

import java.util.Scanner;

public class Exercise5 {

	// 코드의 시작점
	public static void main(String[] args) {

		// 1단계 - 세로 반복 횟수 (for - 횟수)

		// * - 1
		// ** - 2
		// *** - 3
		// **** - 4
		// ***** - 5

		for (int i = 0; i < 5; i++) { // 5번

		}

		// 2단계

		System.out.print("*");
		System.out.println();
		System.out.print("**");
		System.out.println();
		System.out.print("***");
		System.out.println();
		System.out.print("****");
		System.out.println();
		System.out.print("*****");
		System.out.println();

		System.out.println("-----------------------");
		for (int j = 0; j < 1; j++) { // 1번 동작
			System.out.print("*"); // print() 이다
		}
		System.out.println();

		for (int j = 0; j < 2; j++) { // 2번 동작
			System.out.print("*"); // print() 이다
		}
		System.out.println();

		for (int j = 0; j < 3; j++) { // 3번 동작
			System.out.print("*"); // print() 이다
		}
		System.out.println();

		for (int j = 0; j < 4; j++) { // 4번 동작
			System.out.print("*"); // print() 이다
		}
		System.out.println();

		for (int j = 0; j < 5; j++) { // 5번 동작
			System.out.print("*"); // print() 이다
		}
		System.out.println();
		System.out.println("-------------------------------------");

		// 3단계
		
		// outer for
		for (int i = 0; i < 5; i++) { // 5번
			
			// inner for
			// 첫번째 동작 j = 0, j <= 0 ---> inner 한번 동작 -> * -> 줄바꿈
			// 두번째 동작 j = 0, j <= 1 ---> inner 두번 동작 -> ** -> 줄바꿈
			// 세번째 동작 j = 0, j <= 2 ---> inner 세번 동작 -> *** -> 줄바꿈
			// ...
			// ...
			for (int j = 0; j <= i; j++) { // 5번 동작
				System.out.print("*"); // print() 이다
			}
			System.out.println();
		}

	} // end of main

} // end of class

3. 별표 찍기 (이중 for 구문을 활용한 코드를 작성해주세요)

*         - 1 (출력하는거 아님)
***       - 3
*****     - 5
*******   - 7
********* - 9
package basic.exercise;

public class Stars2 {

	public static void main(String[] args) {

		for (int i = 0; i < 10; i += 2) {
			for (int l = 0; l < i; l++) {
				System.out.print("*");
			}
			System.out.println("*");
		}
	}

}

4. 별표 찍기 (이중 for 구문을 활용한 코드를 작성해주세요)

****
***
**
*
package basic.exercise;

public class Stars3 {

	public static void main(String[] args) {

		for (int i = 3; 0 <= i && i < 4; i--) {
			for (int l = 0; l < i; l++) {
				System.out.print("*");
			}
			System.out.println("*");
		}
	}

}

Java 기초 문법 - 1 으로 돌아가기

 

'Java > Java 기초 문법' 카테고리의 다른 글

Java 기초문법 - 1  (0) 2024.05.01
반복문과 조건문 { 연습문제 } - 22  (0) 2024.04.12
break, continue 사용 - 21  (0) 2024.04.12
반복문( while ) - 20  (0) 2024.04.12
반복문( for ) - 19  (0) 2024.04.11