478. 简单计算器

Description

Given two integers a and b, an operator, choices:

+, -, *, /

Calculate a <operator> b.

Example

For a = 1, b = 2, operator = +, return 3.

For a = 10, b = 20, operator = *, return 200.

For a = 3, b = 2, operator = /, return 1. (not 1.5)

For a = 10, b = 11, operator = -, return -1.
public class Calculator {
    /**
     * @param a: An integer
     * @param operator: A character, +, -, *, /.
     * @param b: An integer
     * @return: The result
     */
    public int calculate(int a, char operator, int b) {
        // write your code here
        switch(operator){
            case '+':
                return a+b;
            case '*':
                return a*b;
            case '-':
                return a-b;
                
            case '/':
                return a/b;
                
        }
        return 0;
    }
}
描述
给出两个整数 a , b ,以及一个操作符 opeator

+, -, *, /
返回结果 a<operator>b
posted @ 2019-04-02 22:48  故人叹  阅读(216)  评论(0编辑  收藏  举报