import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class CalculationTest {
/**
* @param args
*/
public static double count(String str){
//1+2*3-5
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js"); /**引用js*/
Object result = null;
try {
result = engine.eval(str);
} catch (ScriptException e) {
e.printStackTrace();
}
System.out.println("结果类型:" + result.getClass().getName() + ",计算结果:" + result);
return (Double) result;
}
public static void main(String[] args) {
count("1+1-5+2*3+8/2"); /**结果类型:java.lang.Double,计算结果:7.0*/
}