[프로그래머스/Java] Lv.0 간단한 식 계산하기

febCho·2024년 3월 15일
0

코딩테스트

목록 보기
88/253
post-thumbnail

문제

문자열 binomial이 매개변수로 주어집니다. binomial은 "a op b" 형태의 이항식이고 a와 b는 음이 아닌 정수, op는 '+', '-', '*' 중 하나입니다. 주어진 식을 계산한 정수를 return 하는 solution 함수를 작성해 주세요.

- 제한사항

  • 0 ≤ a, b ≤ 40,000
  • 0을 제외하고 a, b는 0으로 시작하지 않습니다.

풀이

class Solution {
    public int solution(String binomial) {
        String[] biArr = binomial.split(" ");
        String op = biArr[1];
        
        int a = Integer.parseInt(biArr[0]);
        int b = Integer.parseInt(biArr[2]);
        
        int answer = 0;
        
        if(op.equals("+")) {
            answer = a + b;
        }else if(op.equals("-")) {
            answer = a - b;
        }else{
            answer = a * b;
        }
        
        return answer;
    }
}

결과

profile
Done is better than perfect.

0개의 댓글