1.在pom中引入依赖
<!--四则运算--> <dependency> <groupId>jep</groupId> <artifactId>jep</artifactId> <version>2.24</version> </dependency>
2.调用实现计算
public static void main(String[] args) { System.out.println("开始计算---->");
//这边你可以编写自己需要计算的表达式,我举例直接手动写两个 String test1 = "4*2-(2+1)"; String test2 = "(0.81*0.32)/2-(-3.6+1)"; JEP jep1 = new JEP(); jep1.parseExpression(test1); JEP jep2 = new JEP(); jep2.parseExpression(test2); if (jep1.hasError()||jep2.hasError()) { System.out.println("Error"); } else { //保留两位小数 DecimalFormat df = new DecimalFormat("#.##"); double result1 = jep1.getValue(); System.out.println(df.format(result1)); System.out.println("4*2-(2+1) --> " + result1); double result2 = jep2.getValue(); System.out.println(df.format(result2)); System.out.println("(0.81*0.32)/2-(-3.6+1) --> " + result2); } }
输出结果为
开始计算----> 5 4*2-(2+1) --> 5.0 2.73 (0.81*0.32)/2-(-3.6+1) --> 2.7296
标签:jep,java,--,四则运算,System,jep2,jep1,println,out From: https://www.cnblogs.com/murmansk/p/17828718.html