매일 Algorithm

신재원·2023년 1월 9일
0

Algorithm

목록 보기
1/243

프로그래머스 : 문자열 계산하기 (LEVEV 0)

class Solution {
    // "3 + 4"
    public int solution(String my_string) {

        // split을 하여 공백을 제거했다.
        String[] splitMy_string = my_string.split(" ");
        int answer = Integer.parseInt(splitMy_string[0]);
        
        // i+=2를 하여 x+y 이면은 y를 건너뛰게했다.
        for (int i = 1; i < splitMy_string.length; i+=2) {
            if (splitMy_string[i].equals("+")){
                answer += Integer.parseInt(splitMy_string[i+1]);
            }else{
                answer -= Integer.parseInt(splitMy_string[i+1]);
            }
        }
        return answer;
    }
}

0개의 댓글