将包含算术符号的String转换为整数值

Gow*_*ham 0 java string

    String a = "a*3";
    a = a.replace("a","45");

    // a contains 45*3
Run Code Online (Sandbox Code Playgroud)

你怎么把它转换成输出135.00的DOUBLE?

注意:a可以进行任何关节操作...(+, - ,/)

AJN*_*eld 5

最简单的解决方案是将其传递给Java内置的JavaScript引擎.

public class Eval {

    public static void main(String[] args) throws ScriptException {
        ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");

        engine.put("a", 45);
        Number val = (Number) engine.eval("a*3");
        System.out.println(val.doubleValue());
    }
}
Run Code Online (Sandbox Code Playgroud)