포스트

[Java] 백준 10951번 - A+B - 4

입력 개수가 정해지지 않은 A+B 문제를 Java의 hasNext()로 처리하는 방법을 정리했습니다.

For the English version of this post, see here.
[Java] 백준 10951번 - A+B - 4

BaekJoon 10951

풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        
        while(sc.hasNext()) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            
            System.out.printf("%d\n", a+b);
        }
    }
}

Scanner 클래스에서의 EOF

  • hasNext() 메소드 사용하기
  • 입력된 토큰이 있으면 true 반환, 그렇지 않을 경우 false 반환