(java)프로그래머스 코딩테스트 - 직각삼각형 출력하기

navelop·2023년 11월 13일
0

TIL(CODE)

목록 보기
18/20
import java.util.Scanner;

public class Solution {
    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(); // 한 행 완료시 줄바꿈
        }

    }
}
[결과] ex) 4 입력 시
*
**
***
****

🪄 다른 풀이

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        for(int i=1; i<=n; i++){
            System.out.println("*".repeat(i));
        }
    }
}

repeat 메서드 활용 (java 11이상)
repeat() 메서드는 @**문자열**@을 복사하여, 복사한 새로운 문자열을 반환합니다.
"반복문구".repeat(i) = > "*"을 i번 반복한다

0개의 댓글