[백준] 2438. 별 찍기 - 1

c.Hano·2025년 4월 24일

백준

목록 보기
2/8


중복 for 문을 사용하여 문제를 해결해보자.

첫번째 줄에는 * 이 하나
두번째 줄에는 * 이 두개
(입력받은) N번째 줄에는 * 이 N개 ...

우선 N번째 줄을 설정하는 식을 입력해보자.

Scanner sc = new Scanner(System.in);
int N = sc.nextInt();

for(int a = 1; a <= N; a++) {}

입력받은 N만큼 for문이 반복된다.

그럼 그 안에 N번째 줄에 *을 출력하는 식을 설정해보자.

Scanner sc = new Scanner(System.in);
int N = sc.nextInt();

for(int a = 1; a <= N; a++) {
	for(int j = 1; j <= a; j++) {
    	System.out.print("*");
    }
    System.out.println();
}

N번째 줄에서 N만큼의 *이 출력되고 줄바꿈이 되는 것을 알 수 있다


정답

import java.util.*;

public class Main {
 
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
 
		int N = sc.nextInt();
 
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}
profile
꼬질이

0개의 댓글