如何在java中将两个double值与string类型的关系进行比较?

时间:2018-01-06 14:46:46

标签: java

我需要将两个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

1 个答案:

答案 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);