简体   繁体   English

Java-ScriptEngineManager nashorn Math.random不起作用

[英]Java - ScriptEngineManager nashorn Math.random does not work

i call the follow function with the functionName "random" and the parameter "1 and 50". 我用函数名“ 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;
    }

The content of random.js looks like: random.js的内容如下所示:

  function random(min, max){
    return  Math.floor(Math.random() * (max - min +1)) + min;
  }

The results are never between 1 and 50. It is always more than 100. 结果永远不会在1到50之间。它总是大于100。

If i use it not in java it works. 如果我不在Java中使用它,它将起作用。 Work math from nashorn/javascript oherwise in java? 在Java中从nashorn / javascript学习数学吗?

UPDATE : 更新

My solution is: 我的解决方案是:

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;
}

So i can use parameters with different types. 所以我可以使用不同类型的参数。

I tweaked your example a little bit you can't assign a ScriptEngine to a ScriptEngineManager and it's unclear how you're displaying your random values, or how you're calling random . 我调整你的例子一点点,你无法分配ScriptEngineScriptEngineManager和目前还不清楚你如何显示您的随机值,或者你怎么叫random However, this generates 100 random values between 1 and 50 here 但是,这会在150之间生成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();
    }
}

Adding to Elliot's example there must be something wrong with the parameters you are passing in. The following also generates 100 values between 1 and 50. 添加到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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM