โ ๋ฌธ์
์ฒซ์งธ ์ค์๋ ๋ณ 1๊ฐ, ๋์งธ ์ค์๋ ๋ณ 2๊ฐ, N๋ฒ์งธ ์ค์๋ ๋ณ N๊ฐ๋ฅผ ์ฐ๋ ๋ฌธ์
โ๐ฅ ์ ๋ ฅ
์ฒซ์งธ ์ค์ N(1 ≤ N ≤ 100)์ด ์ฃผ์ด์ง๋ค.
โ๐ฅ ์ถ๋ ฅ
์ฒซ์งธ ์ค๋ถํฐ N๋ฒ์งธ ์ค๊น์ง ์ฐจ๋ก๋๋ก ๋ณ์ ์ถ๋ ฅํ๋ค.
โ
๐ก ํ์ด
์๋ฐ ์ด๋ณด์๋ค์ด ์ด๋ ค์ํ๋ค๋ ์๋ฐ ๋ณ์ฐ๊ธฐ์ด ๋ฌธ์ ๋ ๋ค์ํ ๋ชจ์์ ๋ณ์ฐ๊ธฐ ์ค์์๋ ๋์ด๋๊ฐ ์ฌ์ด ํธ์ด๋ค.
๋ณ์ฐ๊ธฐ ์๋ฆฌ => ์ด์ค for๋ฌธ
๋ฐ๊นฅ for๋ฌธ => ๊ฐ ํ์ด ๋ฐ๋ณต๋๋ ๊ฒ์ ์๋ฏธ(์ค ๋จ์๋ก ์ํ๋๋ ๋ฐ๋ณต)์์ชฝ for๋ฌธ => ํ ํ ์์์์ ๋ก์ง์ ์๋ฏธ(ํ ์ค ๋ด)
ํ๋ค๊ณ ์๊ฐํ๋ฉด ์ฝ๋ค.
โ 1. Scanner
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
โ 2. BufferedReader + BufferedWriter ๋ฅผ ์ด์ฉํ ํ์ด
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
for(int i = 1;i<=n;i++)
{
for(int j = 1;j<=i;j++)
{
bw.write("*");
}
bw.write("\n");
}
br.close();
bw.flush();
bw.close();
}
}
'Algorithm > BaekJoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ฌ๋ฌ๊ฐ์ง ๋ชจ์์ผ๋ก ๋ณ์ฐ๊ธฐ (์๋ฐ for๋ฌธ) (0) | 2024.04.23 |
---|---|
[๋ฐฑ์ค] 2438๋ฒ : ๋ณ ์ฐ๊ธฐ - 2 ( ์๋ฐ / java ) (0) | 2024.04.23 |
[๋ฐฑ์ค] 11022๋ฒ : A+B-8 ์๋ฐ / java (1) | 2024.04.23 |
[๋ฐฑ์ค] 11021๋ฒ : A+B-7 ์๋ฐ / java (0) | 2024.04.23 |
[๋ฐฑ์ค] 15552๋ฒ ๋น ๋ฅธ A+B - ์๋ฐ / java (0) | 2024.04.15 |