用java解析字符串,如字符串"(1+2/5)*3"当成是数值表达式,进行计算出结果来

import java.io.*;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Main
{
public static void main(String[] args) throws Exception
{
String expr = "(1+2/5)*3";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Object result = engine.eval(expr);
System.out.println(result);
}
}

posted @ 2016-12-26 10:29  程序猿001  阅读(3503)  评论(0编辑  收藏  举报