Is there any way to get Boolean's string binding rather than its boolean binding in Java

huangapple go评论128阅读模式
英文:

Is there any way to get Boolean's string binding rather than its boolean binding in Java

问题

以下是您需要翻译的内容:

  1. 这是我需要做的
  2. public void verifyConditionSatisfies(Boolean condition) {
  3. if (condition) {
  4. System.out.println(condition.toString() + "满足条件"); // 这部分是我需要获取其字符串绑定的部分,而不是布尔值的评估版本
  5. } else {
  6. System.out.println(condition.toString() + "不满足条件");
  7. }
  8. }
  9. 以下是预期的用法
  10. verifyConditionSatisfies(10 > 9)
  11. 应该评估为
  12. "10 > 9 满足条件"
英文:

Here's what I need to do:

  1. public void verifyConditionSatisfies( Boolean condition){
  2. if (condition) {
  3. System.out.println(condition.toString()+ "satisfies)//This is the part i need to get its String binding, not boolean evaluated version
  4. }else{
  5. System.out.println(condition.toString()+ " does not satisfy)
  6. }
  7. }

Here's an intended usage:

  1. verifyConditionSatisfies(10>9)

should evaluate as

  1. "10>9 satisfies"

答案1

得分: 1

正如评论中所提到的,无法将布尔表达式10>9直接转换为字符串。将表达式作为字符串"10>9"提供可能更容易实现。

以下是一种非常规的实现方式:

  1. public class BooleanEval {
  2. public static void main(String[] args) throws ScriptException {
  3. System.out.println(new BooleanEvaluation(""10>9""));
  4. System.out.println(new BooleanEvaluation(""25<3""));
  5. }
  6. public static class BooleanEvaluation {
  7. private static final ScriptEngine javaScriptEngine = new ScriptEngineManager().getEngineByName("JavaScript");
  8. private String expression;
  9. private boolean evaluation;
  10. public BooleanEvaluation(String eval) throws ScriptException {
  11. this.expression = eval;
  12. this.evaluation = Boolean.parseBoolean(String.valueOf(javaScriptEngine.eval(expression)));
  13. }
  14. @Override
  15. public String toString() {
  16. String evalString = evaluation ? "满足" : "不满足";
  17. return expression + " " + evalString;
  18. }
  19. }
  20. }

输出:

  1. "10>9" 满足
  2. "25<3" 不满足
英文:

As mentioned in comments, getting a boolean expression 10>9 as a String is not possible. Giving the expression as a String "10>9" might be easier to do it.

Here is an unorthodox way to do it:

  1. public class BooleanEval {
  2. public static void main(String[] args) throws ScriptException {
  3. System.out.println(new BooleanEvaluation("10>9"));
  4. System.out.println(new BooleanEvaluation("25<3"));
  5. }
  6. public static class BooleanEvaluation {
  7. private static final ScriptEngine javaScriptEngine = new ScriptEngineManager().getEngineByName("JavaScript");
  8. private String expression;
  9. private boolean evaluation;
  10. public BooleanEvaluation(String eval) throws ScriptException {
  11. this.expression = eval;
  12. this.evaluation = Boolean.parseBoolean(String.valueOf(javaScriptEngine.eval(expression)));
  13. }
  14. @Override
  15. public String toString() {
  16. String evalString = evaluation ? "satisfies" : "does not satisfy";
  17. return expression + " " + evalString;
  18. }
  19. }
  20. }

Outputs:

  1. 10>9 satisfies
  2. 25<3 does not satisfy

答案2

得分: 0

以下是翻译好的内容:

这个

  1. verifyConditionSatisfies(10 > 9);

被有效地评估为

  1. Boolean temp = 10 > 9;
  2. verifyConditionSatisfies(temp);

该方法不知道其参数是如何计算的。
字符序列1、0、>、9仅存在于源代码中。

因此,答案是“不”。

英文:

This

  1. verifyConditionSatisfies(10>9);

is effectively evaluated as

  1. Boolean temp = 10 > 9;
  2. verifyConditionSatisfies(temp);

The method is unaware of how its argument was calculated.
The character sequence 1, 0, >, 9 exists only in source code.

So, the answer is "no".

huangapple
  • 本文由 发表于 2020年8月14日 20:17:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63412614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定