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

用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);
}
}

你可能感兴趣的:(用java解析字符串,并计算结果。如字符串"(1+2/5)*3"当成是数值表达式,进行计算出结果来...)