Java 德摩根定律
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20043664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
De Morgan's Law
提问by user3003605
I am trying to simplify the following using DeMorgan's Law: ! (x!=0 || y !=0)
我正在尝试使用德摩根定律简化以下内容:(x!=0 || y !=0)
Does x!=0 simplify to x>0? Or am I wrong in the following:
x!=0 是否简化为 x>0?或者我在以下方面错了:
!(x>0 || y>0)
!(x>0) && !(y>0)
((x<=0) && (y<=0))
Thanks.
谢谢。
采纳答案by Alexis C.
Does x!=0 simplify to x>0?
x!=0 是否简化为 x>0?
No that's not true. Because integers are signed.
不,那不是真的。因为整数是有符号的。
How to simplify :
!(x!=0 || y !=0)
?
如何简化:
!(x!=0 || y !=0)
?
Consider this rules :
考虑这个规则:
(second De Morgan's laws)
(第二德摩根定律)
By 1., it implies
由 1.,它意味着
!(x!=0 || y !=0) <=> (!(x!=0)) && (!(y != 0))
!(x!=0 || y !=0) <=> (!(x!=0)) && (!(y != 0))
By 2., it implies
由 2.,它意味着
(!(x!=0)) && (!(y != 0)) <=> (x == 0) && (y == 0)
(!(x!=0)) && (!(y != 0)) <=> (x == 0) && (y == 0)
要进行测试,您可以编写以下循环:
for(int x = -5; x < 5; x++){
for(int y = -5; y < 5; y++){
if(!(x!=0 || y !=0))
System.out.println("True : ("+x+","+y+")");
}
}
回答by Paul Samsotha
Does x!=0 simplify to x>0? Or am I wrong in the following:
x!=0 是否简化为 x>0?或者我在以下方面错了:
x != 0 // reads x does not equal 0; any number BUT 0
x > 0 // reads x is greater than 0; only numbers greater than 0
Do these two look the same, when you write it out like this?
当你这样写的时候,这两个看起来一样吗?
Combined
组合
(x != 0 && x > 0) // any number above 0
(x != 0 || x > 0) // any number BUT 0
回答by Vorsprung
In java integers are always signed so it is not necessarily true that x!=0 is the same as x>0
在 java 中整数总是有符号的,所以 x!=0 不一定与 x>0 相同
回答by brimborium
DeMorgans Law states the following:
DeMorgans 法律规定如下:
!(A & B) = !A | !B (I)
!(A | B) = !A & !B (II)
In your case (II)
applies: !(x>0 || y>0) = !(x>0) && !(y>0) = (x<=0) && (y<=0)
在您的情况下(II)
适用:!(x>0 || y>0) = !(x>0) && !(y>0) = (x<=0) && (y<=0)
PS:I don't understand what you mean by the following. Can you specify?
PS:我不明白你下面的意思。可以指定吗?
"Does x!=0 simplify to x>0?"
“x!=0 是否简化为 x>0?”
回答by Kashif Nazar
The conversion of the first two comparisons according to De' Morgan's law is the following.
根据德摩根定律的前两个比较的转换如下。
!(x>0 || y>0) ---> x <= 0 && y <= 0
!(x>0) && !(y>0) ---> !(x <=0 || y <=0)
回答by John Cupak
When I teach how to write Java do-whileloops, I explain how to write the condition which terminates the loop.
当我教如何编写 Java do-while循环时,我会解释如何编写终止循环的条件。
For example, if I want to ask the user to enter a value which must be 0, 1, 2, or 3, I want the while condition to continue if the input value is not (value >= 0 and value <= 3).
例如,如果我想要求用户输入一个必须是 0、1、2 或 3 的值,如果输入值不是(值 >= 0 和值 <= 3),我希望 while 条件继续.
This translates into while (!(value >= 0) or !(value <= 3)).
这转化为 while (!(value >= 0) 或 !(value <= 3))。
But !(value >= 0) means (value < 0), and !(value <= 3) means (value > 3), so the while loop is written as while (value < 0 || value > 3).
但是 !(value >= 0) 表示 (value < 0),而 !(value <= 3) 表示 (value > 3),所以 while 循环写成while (value < 0 || value > 3)。
Hope this helps.
希望这可以帮助。