코딩테스트/프로그래머스

[프로그래머스] Lv.0 a와 b 출력하기(Java)

선SEON 2023. 10. 17. 15:39

1) 문제

2) 제출 답안

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();

        System.out.println("a = "+ a);
        System.out.println("b = "+ b);
    }
}

" " 따옴표 안  빈 칸 띄어쓰기까지도 고려하기

 

3) 다른 풀이

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();

        System.out.println("a = "+a +"\n" +"b = "+b);
    }
}

출력 문구를 두 번 쓰지 않고 "\n" 을 사용하여 개행함

 

+) 마지막에 sc.close(); 메서드로 닫아주어야

표준입출력(키보드) 가 아닐 시 리소스 낭비, 데이터 누수를 막을 수 있다.