본문 바로가기
알고리즘/백준

[백준 11720번 ː 자바(JAVA)] 숫자의 합

by 그릿er 2020. 5. 7.

 

<문제>

 

N개의 숫자가 공백 없이 쓰여있다. 이 숫자를 모두 합해서 출력하는 프로그램을 작성하시오.

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String num = sc.next();
        int count = 0;
        
        for(int i=0; i<n; i++) {
            count+=Integer.parseInt(Character.toString(num.charAt(i)));
        }
        System.out.println(count);
        sc.close();
        
    }
 }
 
 

 

 

 


 

문자열(String)로 입력을 받은 후 문자열(String)을 문자(Character)로 변환하고 다시 정수(Integer)로 변환하는 게 포인트!

 

※ Character.toString - String을 Char로 형변환

※ Integer.parseInt - Char을 Int로 형변환