๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด(Java)

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค_์ง์‚ฌ๊ฐํ˜• ๋ณ„์ฐ๊ธฐ

by carrot0911 2024. 11. 24.

๋ฌธ์ œ ์„ค๋ช…

https://school.programmers.co.kr/learn/courses/30/lessons/12969?language=java

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

์ด ๋ฌธ์ œ์—๋Š” ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ ๋‘ ๊ฐœ์˜ ์ •์ˆ˜ n๊ณผ m์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
๋ณ„(*) ๋ฌธ์ž๋ฅผ ์ด์šฉํ•ด ๊ฐ€๋กœ์˜ ๊ธธ์ด๊ฐ€ n, ์„ธ๋กœ์˜ ๊ธธ์ด๊ฐ€ m์ธ ์ง์‚ฌ๊ฐํ˜• ํ˜•ํƒœ๋ฅผ ์ถœ๋ ฅํ•ด๋ณด์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • n๊ณผ m์€ ๊ฐ๊ฐ 1,000 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

์˜ˆ์‹œ

์ž…๋ ฅ

5 3

์ถœ๋ ฅ

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

 

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

import java.util.Scanner;

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

        for (int i = 0; i < b; i++) {
            for (int j = 0; j < a; j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

์ฝ”๋“œ ์„ค๋ช…

  • Scanner sc = new Scanner(System.in) : Scanner ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋ฐ›์„ ์ค€๋น„๋ฅผ ํ•œ๋‹ค.
  • int a = nextInt( ) : ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ •์ˆ˜ํ˜• ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์•„ ๋ณ€์ˆ˜ a์— ์ €์žฅํ•œ๋‹ค.
  • int b = nextInt( ) : ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ •์ˆ˜ํ˜• ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์•„ ๋ณ€์ˆ˜ b์— ์ €์žฅํ•œ๋‹ค.
  • for (int i = 0; i < b; i++) { } : ๋ฐ”๊นฅ์ชฝ for๋ฌธ์œผ๋กœ ๋ณ€์ˆ˜ i๊ฐ€ 0๋ถ€ํ„ฐ b - 1๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.
    • for (int j = 0; j < a; j++) { } : ์•ˆ์ชฝ for๋ฌธ์œผ๋กœ ๋ณ€์ˆ˜ j๊ฐ€ 0๋ถ€ํ„ฐ a-1๊นŒ์ง€ ๋ฐ˜๋ณต ์‹คํ–‰ํ•œ๋‹ค.
      • System.out.print("*") : ์ค„๋ฐ”๊ฟˆ ์—†์ด "*"๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. a๊ฐœ์”ฉ ์ถœ๋ ฅ๋œ๋‹ค.
    • System.out.println( ) : ์•ˆ์ชฝ for๋ฌธ์ด ๋๋‚œ ํ›„ ์ค„๋ฐ”๊ฟˆ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.