If not condition 不按预期工作
If not condition doesn't work as expected
我写了一个 if else 条件,它使用 if not(!) 来抛出错误。但是,该条件的行为与我的预期不同,无论当前用户是谁,都会抛出错误:
public void findCorrectUserRole() {
if (Book.name.equals("Test Answers")) {
User currentUser = User.getCurrentUser()
if (currentUser) {
if (!currentUser.hasUserRole("Teacher") || !currentUser.hasUserRole("System Administrator")) {
throw new LCEX("Sorry, only a teacher or System Administrator can edit this.");
}
}
}else{
"Do Something Else"
}
}
您做出了一个无效的假设,即逻辑非运算符对逻辑运算的处理方式与负号在代数中的处理方式相同。有一个名为 DeMorgan's Law 的规则可以帮助您安全地转换逻辑表达式。
按照您编写的代码,用户只有一种方法可以避免出现此异常,用户必须同时具有教师和系统管理员的角色:
groovy:000> a = true; b = false; !a || !b // only one is true -> true
===> true
groovy:000> a = b = false; !a || !b // neither is true -> true
===> true
groovy:000> a = b = true; !a || !b // both are true -> false
===> false
如果使用 DeMorgan 定律重写,这可能会更清楚(从括号中取出否定意味着运算符必须从 ||
更改为 &&
);您的代码等同于:
if (!(currentUser.hasUserRole("Teacher")
&& currentUser.hasUserRole("System Administrator"))) {
“不是当前用户同时拥有教师角色和系统管理员角色”
这绝对不是你想要的。你想要的是
if (!currentUser.hasUserRole("Teacher")
&& !currentUser.hasUserRole("System Administrator")) {
“当前用户没有教师角色,也没有系统管理员角色”
等价地你可以把它写成
if (!(currentUser.hasRole("Teacher")
|| currentUser.hasRole("System Administrator"))) {
“当前用户不是教师或系统管理员角色”
"not (A and B)" is the same as "(not A) or (not B)"
"not (A or B)" is the same as "(not A) and (not B)".
你的if条件错误应该是:
if (!currentUser.hasUserRole("Teacher") && !currentUser.hasUserRole("System Administrator")) {
throw new LCEX("Sorry, only a teacher or System Administrator can edit this.");
}
或
if (!(currentUser.hasUserRole("Teacher") || currentUser.hasUserRole("System Administrator"))) {
throw new LCEX("Sorry, only a teacher or System Administrator can edit this.");
}
现在,如果 currentUser 角色是 "teacher",他不是 "system administrator",那么 if 条件将为真。
我写了一个 if else 条件,它使用 if not(!) 来抛出错误。但是,该条件的行为与我的预期不同,无论当前用户是谁,都会抛出错误:
public void findCorrectUserRole() {
if (Book.name.equals("Test Answers")) {
User currentUser = User.getCurrentUser()
if (currentUser) {
if (!currentUser.hasUserRole("Teacher") || !currentUser.hasUserRole("System Administrator")) {
throw new LCEX("Sorry, only a teacher or System Administrator can edit this.");
}
}
}else{
"Do Something Else"
}
}
您做出了一个无效的假设,即逻辑非运算符对逻辑运算的处理方式与负号在代数中的处理方式相同。有一个名为 DeMorgan's Law 的规则可以帮助您安全地转换逻辑表达式。
按照您编写的代码,用户只有一种方法可以避免出现此异常,用户必须同时具有教师和系统管理员的角色:
groovy:000> a = true; b = false; !a || !b // only one is true -> true
===> true
groovy:000> a = b = false; !a || !b // neither is true -> true
===> true
groovy:000> a = b = true; !a || !b // both are true -> false
===> false
如果使用 DeMorgan 定律重写,这可能会更清楚(从括号中取出否定意味着运算符必须从 ||
更改为 &&
);您的代码等同于:
if (!(currentUser.hasUserRole("Teacher")
&& currentUser.hasUserRole("System Administrator"))) {
“不是当前用户同时拥有教师角色和系统管理员角色”
这绝对不是你想要的。你想要的是
if (!currentUser.hasUserRole("Teacher")
&& !currentUser.hasUserRole("System Administrator")) {
“当前用户没有教师角色,也没有系统管理员角色”
等价地你可以把它写成
if (!(currentUser.hasRole("Teacher")
|| currentUser.hasRole("System Administrator"))) {
“当前用户不是教师或系统管理员角色”
"not (A and B)" is the same as "(not A) or (not B)"
"not (A or B)" is the same as "(not A) and (not B)".
你的if条件错误应该是:
if (!currentUser.hasUserRole("Teacher") && !currentUser.hasUserRole("System Administrator")) {
throw new LCEX("Sorry, only a teacher or System Administrator can edit this.");
}
或
if (!(currentUser.hasUserRole("Teacher") || currentUser.hasUserRole("System Administrator"))) {
throw new LCEX("Sorry, only a teacher or System Administrator can edit this.");
}
现在,如果 currentUser 角色是 "teacher",他不是 "system administrator",那么 if 条件将为真。