본문 바로가기

프로그래머스/lv1

문자열을 정수로 바꾸기

/* 문자열을 정수로 바꾸기
 * 문자열 s를 숫자로 변환한 결과를 반환하는 함수, solution을 완성하세요.
 *
 * 예를들어 str이 "1234"이면 1234를 반환하고, "-1234"이면 -1234를 반환하면 됩니다.
 * str은 부호(+,-)와 숫자로만 구성되어 있고, 잘못된 값이 입력되는 경우는 없습니다.
 */

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class programmer_1_14 {
    static String a1 = "1234";
    static String a2 = "-1234";
    public int solution(String s) {
        int answer = 0;
        return Integer.parseInt(s);
    }
    public static void main(String args[]){
        programmer_1_14 t = new programmer_1_14();
        System.out.println("---------------------------------------");
        System.out.println("result = " + t.solution(a1));
        System.out.println("---------------------------------------");
        System.out.println("result2 = " + t.solution(a2));
        System.out.println("---------------------------------------");
    }
}
cs

'프로그래머스 > lv1' 카테고리의 다른 글

하샤드 수  (0) 2022.12.13
x만큼 간격이 있는 n개의 숫자  (0) 2022.12.13
문자열 내 p와 y의 개수  (0) 2022.12.13
정수 제곱근 판별  (0) 2022.12.13
자연수 뒤집어 배열로 만들기  (0) 2022.12.13