我的程序接受一个字符串输入并使用engine.eval()
来自 ScriptEngine 的导入来计算它。如何将评估值转换为 int 类型?
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Main {
public static void main(String[] args) {
String s = "206 + 4";
Object eval;
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine eng = mgr.getEngineByName("JavaScript");
try {
eval = eng.eval(s);
} catch (ScriptException e) {
System.out.println("Error evaluating input string.");
}
//Convert Object eval to int sum
}
}