AP计算机教程4-3:德摩根定律
德摩根定律由奥古斯塔斯·德摩根(Augustus De Morgan)在十九世纪初发现。它们解释了如何处理复杂条件的取反。
!(a && b) == !a || !b!(a || b) == !a && !b
注意在进行这种类似于分配律的操作后,会把逻辑与&&转换为逻辑或||,反之亦然。
Java使用!作为逻辑非运算符。德摩根定律意味着!(x < 3 && y > 2)这个表达式与(x >= 3 || y <= 2)同时为true。同理!(x < 3 || y > 2)与(x >= 3 && y <= 2)也是等价的。
0:00
What is printed when the following code executes and x equals 4 and y equals 3?
if (!(x < 3 || y > 2)) System.out.println("first case");
else System.out.println("second case");
等价于
x >=3 && y <= 2,其值为false。2
What is printed when the following code executes and x equals 4 and y equals 3?
if (!(x < 3 && y > 2)) System.out.println("first case");
else System.out.println("second case");
等价于
x >=3 || y <= 2,其值为true。1
0 条评论