Java - ScriptEngineManager nashorn Math.random 不工作
Java - ScriptEngineManager nashorn Math.random does not work
我使用函数名 "random" 和参数“1 and 50”调用以下函数。
private String callFunction(String functionName, String[] parameter)
throws FileNotFoundException, ScriptException, NoSuchMethodException {
ScriptEngineManager engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval(new FileReader(myPath + functionName + ".js"));
Invocable invocable = (Invocable) engine;
Object result;
if (parameter == null) {
result = invocable.invokeFunction(functionName);
} else {
result = invocable.invokeFunction(functionName, parameter);
}
System.out.println(result);
return (String) result;
}
random.js的内容如下:
function random(min, max){
return Math.floor(Math.random() * (max - min +1)) + min;
}
结果永远不会在 1 到 50 之间。它总是大于 100。
如果我不在 java 中使用它,它就可以工作。
从 nashorn/javascript 学习数学,否则在 java?
更新:
我的解决方案是:
private String callFunction(String functionName, String parameter)
throws FileNotFoundException, ScriptException, NoSuchMethodException, ClassCastException {
String result = "";
engine.eval(new FileReader(PropertiesHandler.getFullDynamicValuePath() + functionName + ".js"));
if (parameter == null) {
result = (String) engine.eval(functionName + "();");
} else {
result = (String) engine.eval(functionName + parameter + ";");
}
return (String) result;
}
所以我可以使用不同类型的参数。
我稍微调整了您的示例,您无法分配 ScriptEngine
to a ScriptEngineManager
,并且不清楚您如何显示随机值,或者您如何调用 random
。但是,这会在 1
和 50
之间生成 100 个随机值
public static void main(String[] ar) {
String script = "function random(min, max) { "
+ "return Math.floor(Math.random() * (max - min + 1)) + min; }";
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
try {
engine.eval(script);
for (int i = 0; i < 100; i++) {
engine.eval("print(random(1, 50));");
}
} catch (ScriptException e) {
e.printStackTrace();
}
}
添加到 Elliot 的示例中,您传入的参数一定有问题。以下还会生成 1 到 50 之间的 100 个值。
public static void main(String[] ar) {
String script = "function random(min, max) { "
+ "return Math.floor(Math.random() * (max - min + 1)) + min; }";
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
Invocable invocable = (Invocable)engine;
try {
engine.eval(script);
for (int i = 0; i < 100; i++) {
Double result = (Double)invocable.invokeFunction("random", 1, 50);
System.out.println(result.intValue());
}
} catch (ScriptException | NoSuchMethodException e) {
e.printStackTrace();
}
}
我使用函数名 "random" 和参数“1 and 50”调用以下函数。
private String callFunction(String functionName, String[] parameter)
throws FileNotFoundException, ScriptException, NoSuchMethodException {
ScriptEngineManager engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval(new FileReader(myPath + functionName + ".js"));
Invocable invocable = (Invocable) engine;
Object result;
if (parameter == null) {
result = invocable.invokeFunction(functionName);
} else {
result = invocable.invokeFunction(functionName, parameter);
}
System.out.println(result);
return (String) result;
}
random.js的内容如下:
function random(min, max){
return Math.floor(Math.random() * (max - min +1)) + min;
}
结果永远不会在 1 到 50 之间。它总是大于 100。
如果我不在 java 中使用它,它就可以工作。 从 nashorn/javascript 学习数学,否则在 java?
更新:
我的解决方案是:
private String callFunction(String functionName, String parameter)
throws FileNotFoundException, ScriptException, NoSuchMethodException, ClassCastException {
String result = "";
engine.eval(new FileReader(PropertiesHandler.getFullDynamicValuePath() + functionName + ".js"));
if (parameter == null) {
result = (String) engine.eval(functionName + "();");
} else {
result = (String) engine.eval(functionName + parameter + ";");
}
return (String) result;
}
所以我可以使用不同类型的参数。
我稍微调整了您的示例,您无法分配 ScriptEngine
to a ScriptEngineManager
,并且不清楚您如何显示随机值,或者您如何调用 random
。但是,这会在 1
和 50
之间生成 100 个随机值
public static void main(String[] ar) {
String script = "function random(min, max) { "
+ "return Math.floor(Math.random() * (max - min + 1)) + min; }";
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
try {
engine.eval(script);
for (int i = 0; i < 100; i++) {
engine.eval("print(random(1, 50));");
}
} catch (ScriptException e) {
e.printStackTrace();
}
}
添加到 Elliot 的示例中,您传入的参数一定有问题。以下还会生成 1 到 50 之间的 100 个值。
public static void main(String[] ar) {
String script = "function random(min, max) { "
+ "return Math.floor(Math.random() * (max - min + 1)) + min; }";
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
Invocable invocable = (Invocable)engine;
try {
engine.eval(script);
for (int i = 0; i < 100; i++) {
Double result = (Double)invocable.invokeFunction("random", 1, 50);
System.out.println(result.intValue());
}
} catch (ScriptException | NoSuchMethodException e) {
e.printStackTrace();
}
}