매일코테

백준 3단계 no.7

공주맛밤 2022. 7. 20. 11:31

import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        
        for (int i = 0; i<n; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            
            System.out.println("Case #"+(i+1)+": "+(a+b));
        }
    }
}

<오답>

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        
        for (int i = 0; i<n; i++) {
            String a = sc.next();
            String[] str = a.split(" ");
            
            System.out.println("Case #"+(i+1)+": "+(Integer.parseInt(str[0])+Integer.parseInt(str[1]))
        }
    }

"1 2"와 같은 문자열을 .split(" ")하면 배열의 크기가 1이고 요소값이 1인 배열이 생성되는데 이유를 모르겠음

728x90
반응형

'매일코테' 카테고리의 다른 글

백분 3단계 no.9  (0) 2022.07.20
백준 3단계 no.8  (2) 2022.07.20
백준 3단계 no.6  (0) 2022.07.20
백준 3단계 no.5  (0) 2022.07.20
백준 3단계 no.4  (0) 2022.07.20