[javascript] view plain copy
function formula(var1,var2){
return var1+var2-factor
}
java代码如下:
[java] view plain copy
import java.io.FileNotFoundException
import java.io.FileReader
import java.util.Scanner
import javax.script.Bindings
import javax.script.Invocable
import javax.script.ScriptContext
import javax.script.ScriptEngine
import javax.script.ScriptEngineManager
import javax.script.ScriptException
public class AboutJavaScript {
public static void main(String[] args) throws Exception {
ScriptEngine engine=new ScriptEngineManager().getEngineByName("javascript")
Bindings bind = engine.createBindings()
bind.put("factor", 1)
engine.setBindings(bind, ScriptContext.ENGINE_SCOPE)
Scanner input=new Scanner(System.in)
while (input.hasNextInt()) {
int first= input.nextInt()
int sec=input.nextInt()
System.out.println("输入参数是:"+first+","+sec)
engine.eval(new FileReader("F:/model.js"))
if(engine instanceof Invocable){
Invocable in =(Invocable) engine
Double result=(Double) in.invokeFunction("formula", first,sec)
System.out.println("运行结果:"+result.intValue())
}
}
}
}
首先vlaue 这个单词就错了 应该是:value,再测一下,获取的值不明确,最好把value的值也传上去验证,如:DisplayNote('text1','text2',this.value)再改改那个函数