๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ ์ด์•ผ๊ธฐ/JAVA

Java - repeat ๋ฉ”์„œ๋“œ ์ด์šฉํ•ด ๋ณ„์ฐ๊ธฐ

by 0_0๐Ÿ–ค 2023. 12. 20.
๋ฐ˜์‘ํ˜•

 

 

 

๋ณ„ ์ฐ๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋Š” ์ด์ค‘ for๋ฌธ๋งŒ ์‚ฌ์šฉํ–ˆ์—ˆ๋Š”๋ฐ

์ด๋ฒˆ์— ์ƒˆ๋กœ ์•Œ๊ฒŒ ๋œ repeat ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ๊ตฌํ˜„ํ•ด๋ณด์•˜๋‹ค.

์ด์ค‘ for๋ฌธ์€ ์•„๋ฌด๋ž˜๋„ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๋Š” ๋ถ€๋ถ„์ด ์žˆ๋Š”๋ฐ

repeat() ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ํ›จ์”ฌ ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง€๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.

 

 


 

 

repeat()

 

java11 ๋ฒ„์ „๋ถ€ํ„ฐ ์ƒˆ๋กœ ๋‚˜์˜จ String ๋ฉ”์„œ๋“œ๋กœ ๋ฌธ์ž์—ด์„ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’๋งŒํผ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

String.repeat(x); // String์„ x๋งŒํผ ๋ฐ˜๋ณต

 

 

ํŒŒ๋ผ๋ฏธํ„ฐ ์œ ํ˜•์— ๋”ฐ๋ฅธ ์ถœ๋ ฅ ๊ฐ’

 

1. ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ 0์ผ ๊ฒฝ์šฐ ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜

2. ์Œ์ˆ˜์ผ ๊ฒฝ์šฐ IllegalArgumentExceptionthrow ์—๋Ÿฌ๊ฐ’์„ ๋ฐ˜ํ™˜

3. 1์ผ ๊ฒฝ์šฐ String๊ฐ’์„ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜

 

 

// repeat() ์˜ˆ์ œ
System.out.print("hellow".repeat(3));

// ์ถœ๋ ฅ ๊ฒฐ๊ด๊ฐ’
// hellowhellowhellow

 

 

+ repeat()๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ Arrays.fill(), System.arraycopy()๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค

 

 


 

 

 

์ด์ค‘ for๋ฌธ ์ด์šฉํ•œ ๋ณ„ ์ฐ๊ธฐ

 

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException{

        // ๋ณ„ ์ฐ์„ ํ–‰์˜ ์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค.
        int n = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());

        // ์ถœ๋ ฅ ๊ฐ’์ด ๋“ค์–ด๊ฐˆ printValue ๊ฐ์ฒด ์ƒ์„ฑ
        // append๋ฅผ ์ด์šฉํ•  ์˜ˆ์ •์œผ๋กœ StringBuilder ์ด์šฉ
        StringBuilder printValue = new StringBuilder();

        for(int i = 0; i < n; i++){
            for(int j = 0; j < i + 1; j++){
                printValue.append("*");
            }
            printValue.append("\n");
        }
        System.out.println(printValue);
    }
}

 

+ ์—ฐ์‚ฐ์ด๋‚˜ concat์„ ์ด์šฉํ•ด [ * ] ๋ฌธ์ž์—ด์„ ๋”ํ•ด์ค„ ์ˆ˜๋„ ์žˆ์—ˆ์œผ๋‚˜

+, concat์€ ๋ฌธ์ž์—ด์„ ๋”ํ•˜๋ฉฐ ๊ฐ’์ด ๋ฐ”๋€”๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์–ด ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„ ๋ฌธ์ œ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ

๊ฐœ์ธ์ ์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋”ํ•  ๋•Œ StringBuilder๋ฅผ ์ด์šฉํ•˜๋ ค ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ๋‹ค.

 

(์‚ฌ์‹ค ๋ณ„ ์ฐ๋Š” ๊ฒƒ ์ •๋„์˜ ๋ฌธ์ œ๋Š” + ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด ํ’€์–ด๋„ ํฌ๊ฒŒ ์ƒ๊ด€์€ ์—†๊ฒ ์ง€๋งŒ...)

 

 

 

 

 

repeat() ์ด์šฉํ•œ ๋ณ„ ์ฐ๊ธฐ

 

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException{

        int n = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());

        StringBuilder printValue = new StringBuilder();

        for(int i = 0; i < n; i++){
            printValue.append("*".repeat(i + 1)); // i + 1 ๋งŒํผ * ์ถ”๊ฐ€
            printValue.append("\n"); // ์ค„๋ฐ”๊ฟˆ
        }
        System.out.println(printValue);
    }
}

 

--- 5 ์ž…๋ ฅํ–ˆ์„ ๊ฒฝ์šฐ ์ถœ๋ ฅ ๊ฒฐ๊ณผ

*
**
***
****
*****

 

 

 

repeat() ์ด์šฉํ•œ ๋ณ„ ์ฐ๊ธฐ ์˜ˆ์ œ 2

 

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException{

        int n = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());

        StringBuilder printValue = new StringBuilder();

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

 

--- 5 ์ž…๋ ฅํ–ˆ์„ ๊ฒฝ์šฐ ์ถœ๋ ฅ ๊ฒฐ๊ณผ

    *
   **
  ***
 ****
*****

 

 

 

๋ฐ˜์‘ํ˜•