1. ํด๋์ค๋ช ์ Main,
public class Main {
}์ด์๊ฐ์ด ํด๋์ค๋ช ์ Main์ผ๋ก ๋๊ณ ์์ฑํ๋ค
2. Main ํจ์์์ ๋ฐ๋ก ์์ฑ ์, ๋ชจ๋ ๊ฒ์ static์ผ๋ก ์ ์ธ ํ ์์ฑํ๋ค.
public class Main {
private static int max = 0;
private static int n, k;
private static void dfs(int cnt, int num) {
}
}main๋ฌธ ์์ฒด๊ฐ static ํจ์ ์ด๋ฏ๋ก ๋ด๋ถ์์ ์ฌ์ฉํ๋ ์ ์ญ๋ณ์ ๋ฐ ๋ชจ๋ ํจ์ ๋ํ static ์ด์ด์ผ ํ๋ค.
3. ์ ๋ ฅ ๊ฐ์ ๋ฐ์ ๋ Scanner ๋ณด๋จ BufferedReader
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public void solution() throws Exeption {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
}
public static void main(String[] args) throws Exception {
new Main().solution();
}
}Scanner๋ ๋ด๋ถ์ ์ผ๋ก ๋ค์ ์ ๋ ฅ๊ฐ์ ์ฐพ์ ๋ ์ ๊ท์์ ์ฌ์ฉํด ์๋๊ฐ ๋๋ฆฌ๋ค.
4.๋ฌธ์์ด ๊ตฌ๋ถ
String ๋ฌธ์์ด ๊ตฌ๋ถ ์ split()๋ณด๋จ StringTokenizer๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์๋๊ฐ ๋ ๋น ๋ฅด๋ค.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public void solution() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken());
for (int j = 0; j < s; j++) {
int data = Integer.parseInt(st.nextToken());
System.out.println(data);
}
}
}
public static void main(String[] args) throws Exception {
new Main().solution();
}
}์์ ๊ฐ์ด BufferedReader์ StringTokenizer๋ก ์ ๋ ฅ๋ฐ๋๋ค๋ฉด ๋น ๋ฅด๊ฒ ์ ๋ ฅ๋ฐ์ ์ ์๋ค.
5. ์ ๋ ฅ์ ์ํ ํด๋์ค๋ ํ๋๋ง
- System.in์ด ๋ค์ด๊ฐ ํด๋์ค๋ ๋จ ํ๋๋ง ์์ฑํ๋ ๊ฒ์ด ์ข๋ค.
6. ์ถ๋ ฅ ์ System.out.printIn() ๋ณด๋ค BufferedWriter๊ฐ ๋น ๋ฅด๋ค
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
public void solution() throws Exception {
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 = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine()); // ํ ํฐํ
int s = Integer.parseInt(st.nextToken());
for (int j = 0; j < s; j++) {
int data = Integer.parseInt(st.nextToken());
bw.write(String.valueOf(data)); // ์ถ๋ ฅ
bw.newLine(); // ์ค๋ฐ๊ฟ }
}
bw.flush(); // ์ถ๋ ฅ ๋ฒํผ ๋น์ฐ๊ธฐ
br.close(); // ์์ ํด์
bw.close(); // ์์ ํด์
}
public static void main(String[] args) throws Exception {
new Main().solution();
}
}