java 根据任何公式,得到结果

尾号0505 2017-05-19 11:04:06
java有没有工具类能根据公式和参数得到结果的,公式和参数都是入参;

例如:参数公式为a+b>c,参数a=1,b=2,c=3; 工具类返回false;
参数公式为d>a+b>c,参数a=1,b=2,c=4,d=1; 工具类返回true;
参数公式为a+b,参数a=1,b=2 ;工具类返回3;
...全文
355 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
--sharp 2019-11-12
  • 打赏
  • 举报
回复
public static void main(String[] args) throws ScriptException { /* 使用JS脚本引擎执行 */ ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine sEngine = sem.getEngineByName("javascript"); System.out.println( sEngine.eval("(22+12-1)/10")); } pint:3.3
110成成 2017-05-19
  • 打赏
  • 举报
回复
写了一个,参考下 首先是替换,然后转换为计算式 里面异常条件没处理,你可以参考下 public static void main(String[] args) throws ScriptException { Map<String, String> map = new HashMap<String, String>(); /* map.put("a", "1"); map.put("b", "2"); map.put("c", "4"); map.put("d", "1"); System.out.println(StringTojisuanshi(format("d>a+b>c", map))); map.put("a", "1"); map.put("b", "2"); map.put("c", "3"); System.out.println(StringTojisuanshi(format("a+b>c", map))); */ map.put("a", "1"); map.put("b", "2"); System.out.println(StringTojisuanshi(format("a+b", map))); } /* * 参数公式为a+b>c,参数a=1,b=2,c=3; 工具类返回false; 参数公式为d>a+b>c,参数a=1,b=2,c=4,d=1; * 工具类返回true; 参数公式为a+b,参数a=1,b=2 ;工具类返回3; */ public void getCount(String str, Map map) { } public static String format(String input, Map<String, String> map) { for (Map.Entry<String, String> entry : map.entrySet()) { input = input.replace(entry.getKey(), entry.getValue()); } return input; } public static String StringTojisuanshi(String str) throws ScriptException { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("js"); Object result = engine.eval(str); return result.toString(); }
一个治疗术 2017-05-19
  • 打赏
  • 举报
回复
这种东西自己写一个不就好了吗
MikeDDT009 2017-05-19
  • 打赏
  • 举报
回复
不存在,真有那么智能那就不科学了,只能弄上符合条件的,特定的,而且需要你自己写。不存在什么万能对象来解决这个问题

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧