Generally involved in the calculation will be used in the configuration, in the background to configure, but basically will be tangled in how to pass the value in and determine the output data
Package com.study.day01;
Import Javax.script.ScriptEngine;
Import Javax.script.ScriptEngineManager;
Import javax.script.ScriptException;
public class Expresstest {
public static void Test1 () throws Scriptexception {
STR is a formula defined in the database
String str = "X<=1?5:X<=2?10:X<=5?15:20";
Scriptenginemanager manager = new Scriptenginemanager ();
ScriptEngine engine = Manager.getenginebyname ("JS");
The form of a key-value pair will (7), where 7 is a constant, but the value in the project is a variable
Engine.put ("X", 7);
Object result = Engine.eval (str);
SYSTEM.OUT.PRINTLN ("Result type:" + Result.getclass (). GetName () + ", calculation result:"
+ result);
}
public static void Main (string[] args) throws Scriptexception {
Test1 ();
}
}
Java turns a string into an arithmetic expression