직각삼각형 출력하기

이윤설·2024년 2월 15일
0

제출코드

import java.util.Scanner;

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

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < n; i++) { 
            for (int j = 0; j <= i; j++) { 
                sb.append("*");
            }
            sb.append(System.lineSeparator());
        }

        System.out.println(sb);
    }
}

모범답안

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(n)함수를 통해 문자열을 n번 반복하여 출력할 수 있다.
profile
화려한 외면이 아닌 단단한 내면

0개의 댓글