본문 바로가기

알고리즘

입력 횟수 만큼 출력문 출력하기

import java.util.*;
class Main {
	public static Scanner scanner = new Scanner(System.in);
	public static void main(String[] args) {
		
		int n;
		n = scanner.nextInt(); //n = "Goorm" 을 출력할 횟수
		
		for(int i = 0; i < n; i++) {
			System.out.print("Goorm");
			if(i != n - 1){
				//i가 마지막 실행 직전까지
				System.out.print(", ");
			}
		}
	}
}

 

업그레이드 ver.

import java.util.*;
class Main {
	public static Scanner scanner = new Scanner(System.in);
	public static void main(String[] args) {
		
		int n = scanner.nextInt(); //n = "Goorm" 을 출력할 횟수
		
		for(int i = 0; i < n; i++) {
			String lastComma = (i != n - 1) ? ", " : "";
			System.out.print("Goorm" + lastComma);
		
		}
	}
}

문제 출처 : goormedu - 10주 완성 알고리즘 코딩테스트