포스트

[Java] Baekjun No. 10952 - A+B - 5

0 We have summarized the Java while(true) solution and termination condition that repeats until 0 is entered.

한국어 원문은 여기에서 볼 수 있습니다.
[Java] Baekjun No. 10952 - A+B - 5

BaekJoon 10952

Solution

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

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

In Java, while(true) is usually used, not while(1) like in Python.

  • In Java, the conditional expression must be of type boolean.
  • In Java, 1 is int, so it is not automatically converted to boolean.

  • Handled implicitly in Python as 0(False), 1(True)