GroovyShell 实现
public static void main(String args[]) {

        Binding binding = new Binding();

        binding.setVariable("F",2.5);
binding.setVariable("T",30);
binding.setVariable("A",100);
binding.setVariable("P0",100); binding.setVariable("language", "Groovy"); GroovyShell shell = new GroovyShell(binding); Object F1 =shell.evaluate("P1=(1+0.1 * (F/100) * T)*P0; return P1 "); Object F2 =shell.evaluate("P1=P0*(0.055*0.20+1.0011)+A; return P1 "); System.out.println(F1);
System.out.println(F2); }
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.7</version>
</dependency>
ScriptEngine实现
public static void main(String args[]) {

        ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
Compilable compilable = (Compilable) engine;
Bindings bindings = engine.createBindings(); //Local级别的Binding
String script = "(1+0.1 * (F/100) * T)*P0"; //定义函数并调用
CompiledScript JSFunction = null; //解析编译脚本函数
try {
JSFunction = compilable.compile(script);
bindings.put("F", 2.5);
bindings.put("T", 30);
bindings.put("A", 100);
bindings.put("P0", 100);
Object result = JSFunction.eval(bindings);
System.out.println(result); //调用缓存着的脚本函数对象,Bindings作为参数容器传入
} catch (ScriptException e) {
e.printStackTrace();
} }

最新文章

  1. 简单的JavaScript互斥锁
  2. php对二维数组进行相关操作(排序、转换、去空白等)
  3. AndroidUI优化工具——HierarchyViewer
  4. cent os下面的基本配置操作
  5. JUnit 4 如何正确测试异常
  6. 正则表达式删除指定的HTML 标签
  7. 【mysql的编程专题】触发器
  8. [每日一题] 11gOCP 1z0-053 :2013-10-11 Flashback Data Archive属性.........................43
  9. Linux中的读函数与块高速缓存
  10. 使用promise手动封装ajax函数
  11. Win7 IE11无法打开的可能解决办法
  12. 二叉查找树(Binary Sort Tree)(转)
  13. java 学习笔记2 面向对象(上)
  14. AttributeError: &#39;NoneType&#39; object has no attribute &#39;split&#39; 报错处理
  15. php代码审计——DVWA
  16. [C++] C语言及C++语言中包含的头文件名称,及作用
  17. Java 8 的 Metaspace
  18. 谈谈MySQL的do语句
  19. 3D 特征点概述(1)
  20. Centos7上安装docker及使用scrapy-splash

热门文章

  1. Linux iptables常用命令
  2. Proguard语法及常用proguard.cfg代码段
  3. socket发送http请求
  4. 自定义控件VS用户控件
  5. Redis的字典扩容与ConcurrentHashMap的扩容策略比较
  6. Unix系统编程()malloc和free的实现
  7. 16c554 的头文件
  8. 编译FFmpeg for iOS
  9. 关于Unity的C#基础学习(五)
  10. 在其模块列表中有一个错误模块“ManagedPipelineHandler”。