Dev538

[백준] [JAVA/10951번] A+B - 4 본문

Algorithm

[백준] [JAVA/10951번] A+B - 4

Dev538 2019. 12. 26. 01:02

문제

두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.


입력

입력은 여러 개의 테스트 케이스로 이루어져 있다.

각 테스트 케이스는 한 줄로 이루어져 있으며, 각 줄에 A와 B가 주어진다. (0 < A, B < 10)


출력

각 테스트 케이스마다 A+B를 출력한다.


풀이 설명

반복문 While 문을 사용하여 10952 문제와 다르게 0 0 이라는 종료가 없다.

이렇게되면 프로그램이 종료가 되는 시점을 찾지못하여 무제한으로 데이터를 입력받기위해 대기하거나 기다릴것이다.

 

이 문제는 While(반복문) + EOF(End of File) 의 문제로 데이터 입력이 끝나거나 마지막인지 확인하여 종료하는 조건을 추가하도록 한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
import java.io.*;
 
public class baekjoon_10951 {
    public static void main(String[] args){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = null;
        String temp[];
        int num = 0;
        String read ="";
        try{
 
            while((read = br.readLine()) != null && read.length() > 0) {
                temp = read.split(" ");
 
                num = Integer.parseInt(temp[0]) + Integer.parseInt(temp[1]);
                bw.write(num + "\n");
 
            }
            bw.flush();
 
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            if(br!=null)try{br.close();}catch(IOException e){}
            if(bw!=null)try{bw.close();}catch(IOException e){}
        }
    }
}
 
 

 

Comments