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

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

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

问题

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

这是我需要做的

public void verifyConditionSatisfies(Boolean condition) {

    if (condition) {
        System.out.println(condition.toString() + "满足条件"); // 这部分是我需要获取其字符串绑定的部分,而不是布尔值的评估版本
    } else {
        System.out.println(condition.toString() + "不满足条件");
    }

}

以下是预期的用法

verifyConditionSatisfies(10 > 9)

应该评估为

"10 > 9 满足条件"
英文:

Here's what I need to do:

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

	}

Here's an intended usage:

verifyConditionSatisfies(10>9)

should evaluate as

"10>9 satisfies"

答案1

得分: 1

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

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

public class BooleanEval {
    public static void main(String[] args) throws ScriptException {
        System.out.println(new BooleanEvaluation(""10>9""));
        System.out.println(new BooleanEvaluation(""25<3""));
    }

    public static class BooleanEvaluation {
        private static final ScriptEngine javaScriptEngine = new ScriptEngineManager().getEngineByName("JavaScript");
        private String expression;
        private boolean evaluation;

        public BooleanEvaluation(String eval) throws ScriptException {
            this.expression = eval;
            this.evaluation = Boolean.parseBoolean(String.valueOf(javaScriptEngine.eval(expression)));
        }

        @Override
        public String toString() {
            String evalString = evaluation ? "满足" : "不满足";
            return expression + " " + evalString;
        }
    }
}

输出:

"10>9" 满足
"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:

public class BooleanEval {
	public static void main(String[] args) throws ScriptException {
		System.out.println(new BooleanEvaluation("10>9"));
        System.out.println(new BooleanEvaluation("25<3"));
	}

	public static class BooleanEvaluation {
		private static final ScriptEngine javaScriptEngine = new ScriptEngineManager().getEngineByName("JavaScript");
		private String expression;
		private boolean evaluation;

		public BooleanEvaluation(String eval) throws ScriptException {
			this.expression = eval;
			this.evaluation = Boolean.parseBoolean(String.valueOf(javaScriptEngine.eval(expression)));
		}

		@Override
		public String toString() {
			String evalString = evaluation ? "satisfies" : "does not satisfy";
			return expression + " " + evalString;
		}
	}
}

Outputs:

10>9 satisfies
25<3 does not satisfy

答案2

得分: 0

以下是翻译好的内容:

这个

  verifyConditionSatisfies(10 > 9);

被有效地评估为

  Boolean temp = 10 > 9;
  verifyConditionSatisfies(temp);

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

因此,答案是“不”。

英文:

This

  verifyConditionSatisfies(10>9);

is effectively evaluated as

  Boolean temp = 10 > 9;
  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:

确定