Convert String to operator(+*/-) in java

前端 未结 5 1253
时光说笑
时光说笑 2021-01-06 05:57

I am using Stack class to calculate simple arithmetic expressions involving integers, such as 1+2*3.your program would execute operations in the order given,without regardin

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-06 06:46

    Quick solution: Use below code for executing correct javascript Arithmetic expression in java.

        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine se = manager.getEngineByName("JavaScript");        
        try {
            Object result = se.eval(val);
            System.out.println(result.toString());
        } catch (ScriptException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    

提交回复
热议问题