从Java 6 开始,Java内置了Javascript引擎。
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class hello {
public static void main(String [] args) throws ScriptException {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
System.out.println(engine.eval("123+456"));
}
}
或者
System.out.println(new ScriptEngineManager().getEngineByName("JavaScript").eval(scanner.nextLine()).toString());