导入包
<!-- aviator -->
<dependency>
<groupId>com.googlecode.aviator</groupId>
<artifactId>aviator</artifactId>
<version>4.2.10</version>
</dependency>
先看代码
Long result = (Long) AviatorEvaluator.execute("(10+1)+(8+2)");
System.out.println(result);
Map<String,Object> map = new HashMap<>();
map.put("a",12);
map.put("b",15);
map.put("c",15);
Object execute = AviatorEvaluator.compile("(a+b)+c").execute(map);
System.out.println(execute);
实现计算器功能
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("======输入表达式如(1+2)*2====");
String next = scanner.next();
String value = AviatorEvaluator.execute(next).toString();
System.out.println("结果:"+value);
}