Ad
Code
Diff
  • interface Calculator {
      static int calculate(int a, int b, char operation) {
        int result = 0;
        switch(operation) {
            case '+':
            result = a + b; break;
            case '-':
            result = a - b; break;
            case '*':
            result = a * b; break;
            default:
            System.exit(0); break;
        }
        return result;
      }
    }
    
    • import javax.script.ScriptEngineManager;
    • interface Calculator {
    • static int calculate(int a, int b, char operation) {
    • var engine = new ScriptEngineManager().getEngineByName("JavaScript");
    • try {
    • return Integer.parseInt(String.valueOf(engine.eval(a + " " + operation + " " + b)));
    • } catch (Exception cause) {
    • return 0;
    • int result = 0;
    • switch(operation) {
    • case '+':
    • result = a + b; break;
    • case '-':
    • result = a - b; break;
    • case '*':
    • result = a * b; break;
    • default:
    • System.exit(0); break;
    • }
    • return result;
    • }
    • }