我需要将两个double值与关系类型作为字符串进行比较。 例如:
http://yourwebsite.com/moodle/webservice/rest/server.php?moodlewsrestformat=json&wstoken=YOUR_TOKEN&wsfunction=mod_quiz_get_attempt_data&attemptid=1&page=0
在上面的例子中需要使用java
来比较aAA关系bBB答案 0 :(得分:1)
您可以创建Map
将字符串映射到实际操作,例如为BiPredicate
。
Map<String, BiPredicate<Double, Double>> relations = new HashMap<>();
relations.put("<=", (a, b) -> a <= b);
relations.put(">=", (a, b) -> a >= b);
relations.put("==", (a, b) -> a == b);
// ...
String relation = "<=";
double aAA=5.9,bBB=6.999999;
boolean result = relations.get(relation).test(aAA, bBB);