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

[프로그래머스] Lv.0 덧셈식 출력하기(Java)

선SEON 2023. 10. 18. 15:47

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 + " + " + b + " = " + (a + 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.printf("%d + %d = %d",a,b,a+b);
    }
}

printf : 서식이 있는 출력

%d : 정수

%f : 실수

%c : 문자

%s : 문자열

 

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();
        int sum = a + b;

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