๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

repeat()1

Java - repeat ๋ฉ”์„œ๋“œ ์ด์šฉํ•ด ๋ณ„์ฐ๊ธฐ ๋ณ„ ์ฐ๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋Š” ์ด์ค‘ 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).. 2023. 12. 20.