Java怎么将一个含有逻辑运算符的字符串转化成逻辑表达式

原文链接:http://bbs.csdn.net/topics/360244167


package cn.com.zhang.test;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class CalStringTest {

	public static void main(String[] args) {
		String str = "(1+2)*3+9/5-5+8*3.9+7/3";
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("js");
		Object result = null;
		try {
			result = engine.eval(str);
		} catch (ScriptException e) {
			e.printStackTrace();
		}

		System.out.println("结果类型:" + result.getClass().getName() + ",计算结果:" + result);
	}

}

结果类型:java.lang.Double,计算结果:39.333333333333336

你可能感兴趣的:(java)