英文:
Create correct expression with java
问题
我遇到了一个问题,需要你的帮助。我有以下情景:
我有三个值:
- 布尔值 visible
- 整数 views
- 整数 executions
现在,我想要创建一个表达式,将数据库中的 hidden
设置为 true 或 false。
我希望实现以下情况:
- 如果 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:
- 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 && (views <=4 || executions <=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 <= 4 OR executions <= 5 then hidden = false
and after adding parentheses to adhere to the Precedence of Logical Operators
if visible is true AND (views <= 4 OR executions <= 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 && (views<=4 || executions<=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>4 && executions>5;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论