본문 바로가기
Algorithm/Programmers

[Programmers] Lv.0 / 문자열 반복해서 출력하기 / Java

by unknownomad 2023. 10. 17.

문제

 

풀이

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
    
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int n = sc.nextInt();
        
        for(int i = 0; i < n; i++) {
            System.out.print(str);
        }
        sc.close();
    }
}
class Main {
    public static void main(String[] args) {
        
        String s = "When I suddenly look back";
        for (int i = 0; i < s.length(); i++) {
            System.out.print(s.charAt(i));
        }
    }
}
class Main {
    public static void main(String[] args) {
    
        String s = "When I suddenly look back";
        char[] chars = s.toCharArray();
        for (char ch: chars) {
            System.out.print(ch);
        }
    }
}
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
 
class Main {
    public static void main(String[] args) {
    
        String s = "When I suddenly look back";
        CharacterIterator it = new StringCharacterIterator(s);
        while (it.current() != CharacterIterator.DONE) {
            System.out.print(it.current());
            it.next();
        }
    }
}
class Main {
    public static void main(String[] args) {
    
        String s = "When I suddenly look back";
        String[] arr = s.split("");
        for (String ch: arr) {
            System.out.print(ch);
        }
    }
}

 

출처

https://school.programmers.co.kr/learn/courses/30/lessons/181950

https://www.techiedelight.com/ko/iterate-over-characters-string-java/

댓글