使用Java创建正确的表达式

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

Create correct expression with java

问题

我遇到了一个问题,需要你的帮助。我有以下情景:

我有三个值:

  • 布尔值 visible
  • 整数 views
  • 整数 executions

现在,我想要创建一个表达式,将数据库中的 hidden 设置为 true 或 false。

我希望实现以下情况:

  1. 如果 visible 为 true,并且 views <= 4 或 executions <= 5,则 hidden = false

我的代码如下:

calculateHiddenFlag(boolean visible, int views, int executions) {
    return visible && (views <= 4 || executions <= 5);
}

有人可以帮助我吗?

单元测试:

@Test
public void calculateHiddenFlag_false_if_hidden_false() {
    assertFalse(ImportHelper.calculateHiddenFlag(false, 0, 0));
}

@Test
public void calculateHiddenFlag_true_if_hidden_true_and_views_leq4() {
    assertTrue(ImportHelper.calculateHiddenFlag(true, 4, 0));
}

@Test
public void calculateHiddenFlag_true_if_hidden_true_and_executions_leq5() {
    assertTrue(ImportHelper.calculateHiddenFlag(true, 0, 5));
}

@Test
public void calculateHiddenFlag_false_if_hidden_true_but_views_gt4() {
    assertFalse(ImportHelper.calculateHiddenFlag(true, 10, 0));
}

@Test
public void calculateHiddenFlag_false_if_hidden_true_but_executions_gt5() {
    assertFalse(ImportHelper.calculateHiddenFlag(true, 0, 10));
}
英文:

I'm sitting on a problem and need your help. I have the following scenario:

I have three values:

boolean visible
int views
int executions

Now, I d'like to create a expression which sets hidden in my DB to true or false.

I should get this:

  1. If visible is true, views <=4 or executions <=5 then hidden = false

My code looks like this:

calculateHiddenFlag(boolean visible, int views, int executions) {
    return visible &amp;&amp; (views &lt;=4 || executions &lt;=5);
}

Can someone support me?

Unit tests:

    @Test
public void calculateHiddenFlag_false_if_hidden_false() {
    assertFalse(ImportHelper.calculateHiddenFlag(false, 0, 0));
}

@Test
public void calculateHiddenFlag_true_if_hidden_true_and_views_leq4() {
    assertTrue(ImportHelper.calculateHiddenFlag(true, 4, 0));
}

@Test
public void calculateHiddenFlag_true_if_hidden_true_and_executions_leq5() {
    assertTrue(ImportHelper.calculateHiddenFlag(true, 0, 5));
}

@Test
public void calculateHiddenFlag_false_if_hidden_true_but_views_gt4() {
    assertFalse(ImportHelper.calculateHiddenFlag(true, 10, 0));
}

@Test
public void calculateHiddenFlag_false_if_hidden_true_but_executions_gt5() {
    assertFalse(ImportHelper.calculateHiddenFlag(true, 0, 10));
}

答案1

得分: 2

(根据下面的评论进行了编辑)

初始语句应解释为

“如果visible为true且views≤4,或者executions≤5,则hidden为false。”

在添加括号以遵循逻辑运算符的优先级后,应该是:

“如果visible为true且(views≤4或者executions≤5),则hidden为false。”

所以代码在字面上读起来就是你说的:

calculateHiddenFlag(boolean visible, int views, int executions) {
    return !(visible && (views <= 4 || executions <= 5));
}

或者,可以使用两次德摩根定律,并根据逻辑运算符的优先级移除括号,改写为:

calculateHiddenFlag(boolean visible, int views, int executions) {
    return !visible || (views > 4 && executions > 5);
}
英文:

(Edited according to comment below)

The initial statement should interpreted as

if visible is true AND views &lt;= 4 OR executions &lt;= 5 then hidden = false

and after adding parentheses to adhere to the Precedence of Logical Operators

if visible is true AND (views &lt;= 4 OR executions &lt;= 5) then hidden = false

So the code would be literally what you say when reading it:

calculateHiddenFlag(boolean visible, int views, int executions) {
    return !(visible &amp;&amp; (views&lt;=4 || executions&lt;=5));
}

Or alternatively using some of De Morgan's laws two times and removing parentheses according to Precedence of Logical Operators

calculateHiddenFlag(boolean visible, int views, int executions) {
    return !visible || views&gt;4 &amp;&amp; executions&gt;5;
}

huangapple
  • 本文由 发表于 2020年9月15日 22:21:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63903924.html
匿名

发表评论

匿名网友

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

确定