Simple Calculator

题目:

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

分析:

这是个syntax题目,并不是算法题目

解法:

public class Calculator {
    /**
      * @param a, b: Two integers.
      * @param operator: A character, +, -, *, /.
      */
    public int calculate(int a, char operator, int b) {
        /* your code */
        int res = 0;
        switch (operator) {
            case '+':
                res = a + b;
                break;
            case '-':
                res = a - b;
                break;
            case '*':
                res = a * b;
                break;
            case '/':
                if (b == 0) {
                    break;
                }
                res = a / b;
                break;
            default:
        }
        return res;
    }
}

results matching ""

    No results matching ""