1

我在 DB 中有 2 个字段,例如senti_opsenti_score它们都是 Strings 。senti_score包含值Doubleie等字符串25.0030.50senti_op包含关系运算符(如>=,>等)的字符串<

在Java中,我想这样做:

  • 对于senti_op >=,我希望逻辑表达式为:if ("some double value" >= senti_score)
  • 因为<,我想要if ("some double value" < senti_score)

我正在尝试形成这些关系表达式并让它们的boolean结果稍后被代码的其他部分使用。

请提供可编译的 Java 示例代码。我试图避免为每个运算符使用字符串比较编写大量 if-else 语句,并评估值并寻找小型且有效的解决方案。

我对这些事情很陌生,并感谢您的所有帮助。

4

3 回答 3

4
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class SimpleTests {

    public static void main(String[] args) {
            ScriptEngineManager mgr = new ScriptEngineManager();
            ScriptEngine engine = mgr.getEngineByName("JavaScript");
            String exp = "some double value"+ senti_op +senti_score;
            try {
                System.out.println(engine.eval(exp));
            } catch (ScriptException e) {

                e.printStackTrace();
            }


    }
}
于 2013-06-23T08:04:53.593 回答
1

未测试(带有一些lombok注释和一些guava

@RequiredArgsConstructor
private static enum Operation {
    LESS_OR_EQUAL("<="), GREATER_OR_EQUAL(">="), 
    GREATER(">"), LESS("<"), EQUALS("=");

    @Getter
    final String operation;

    public static Operation parse(final String val) {
        for(final Operation pop : values()) {
            if (pop.getOperation().equals(val)) {
                return pop;
            }
        }
        return null;
    }
}

public boolean handleOperation(final String senti_score, final String senti_op, final Double someDoubleVal) {
    // parse the double
    final Double sentiScore = Doubles.tryParse(senti_score);
    // parse the operation using Operation enum
    final Operation sentiOp = Operation.parse(senti_op);

    // TODO: checks for nullity on both parsed value

    // use a switch to compute result
    switch (sentiOp) {
    case LESS_OR_EQUAL:
        return someDoubleVal.compareTo(sentiScore) <= 1; 
    case GREATER_OR_EQUAL:
        return someDoubleVal.compareTo(sentiScore) >= 1;
    case LESS:
        return someDoubleVal.compareTo(sentiScore) < 1;
    case GREATER:
        return someDoubleVal.compareTo(sentiScore) > 1;
    case EQUAL:
        return someDoubleVal.compareTo(sentiScore) == 0;
    }
    throw new IllegalArgumentException("Unhandled op:" + senti_op);
}
于 2013-06-23T08:10:07.313 回答
1

在 Java 7 中,您可以避免使用 String 上的 switch 语句编写 if-else 链。

您还应该研究浮点运算。它并不总是表现得像人们预期的那样。

public class SentiEvaluator {

    public boolean evaluate(String sentiOp, String sentiScore, double otherValue) {
        if (sentiOp == null) {
            throw new IllegalArgumentException("Operator must be specified.");
        }
        if (sentiScore == null) {
            throw new IllegalArgumentException("Score must be specified.");
        }

        double score = Double.parseDouble(sentiScore);
        return evaluateJava7(score, sentiOp, otherValue);
    }

    private boolean evaluateJava7(double leftOp, String operator, double rightOp) {
        switch (operator) {
            case "=": return leftOp == rightOp;
            case ">": return leftOp > rightOp;
            case ">=": return leftOp >= rightOp;
            case "<": return leftOp < rightOp;
            case "<=": return leftOp <= rightOp;
            default: throw new IllegalArgumentException("Unknown operator " + operator);
        }
    }

}
于 2013-06-23T08:29:17.800 回答