使用 NOT .equalsIgnoreCase() 的 JAVA 循环逻辑错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17912331/
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
JAVA looping logic error with NOT .equalsIgnoreCase()
提问by Kaptain
I'm trying to use equalsIgnoreCase() in a while loop to try and check if something other than what was intended to be written was written by using the NOT (!) operator. For example:
我正在尝试在 while 循环中使用 equalsIgnoreCase() 来尝试检查是否使用 NOT (!) 运算符编写了不打算写入的内容。例如:
String temp="A";
boolean x =(!temp.equalsIgnoreCase("a")) ;
See, this works with a while loop. If it's not A, it will keep looping but this next line does not
看,这适用于 while 循环。如果不是 A,它将继续循环,但下一行不会
boolean x =(!temp.equalsIgnoreCase("a") || !temp.equalsIgnoreCase("b")) ;
This does not seem to work anymore. This returns true, no matter what you type, even if it is a or b. So when I use the whole line of code to check for any of the letters that are not suppose to be used:
这似乎不再起作用了。无论您键入什么,这都会返回 true,即使它是 a 或 b。因此,当我使用整行代码检查任何不应该使用的字母时:
while (!temp.equalsIgnoreCase("A") || !(temp.equalsIgnoreCase("B")) ||!temp.equalsIgnoreCase("D")|| !temp.equalsIgnoreCase("P") || !temp.equalsIgnoreCase("S"))
{ ***Do Code***}
it loops whatever you put in, even if it will equal one of the letters.
它会循环您输入的任何内容,即使它等于其中一个字母。
When there is more than one !temp.equalsIngnoreCase, the code does't work with OR (||).
当有多个 !temp.equalsIngnoreCase 时,代码不能使用 OR (||)。
The only way I can get it to work is if I change the OR to AND
我可以让它工作的唯一方法是将 OR 更改为 AND
while (!temp.equalsIgnoreCase("A") && !(temp.equalsIgnoreCase("B")) && !temp.equalsIgnoreCase("D")&& !temp.equalsIgnoreCase("P") && !temp.equalsIgnoreCase("S"))
Even though I seem to have found a solution, I still don't understand why OR doesn't work but AND does. I removed the NOT to see if everything works, and it seems to loop perfectly when one of the letters is entered.
尽管我似乎找到了解决方案,但我仍然不明白为什么 OR 不起作用而 AND 起作用。我删除了 NOT 以查看是否一切正常,当输入其中一个字母时,它似乎完美循环。
回答by Jon Skeet
it loops whatever you put in, even if it will equal one of the letters.
它会循环您输入的任何内容,即使它等于其中一个字母。
Yes, of course it does.
是的,当然可以。
You're asking it to keep going while it isn't A
orit isn't B
. Well nothing can be bothA
and B
... if the value is equal to B
then it won't be equal to A
so the first operand will keep the loop going. If the value is equal to A
then it won't be equal to B
so the second operand will keep the loop going.
你要求它在它不是A
或不是时继续前进B
。好吧,没有什么可以同时A
和B
...如果值等于B
那么它不会等于A
所以第一个操作数将继续循环。如果该值等于A
那么它将不等于B
所以第二个操作数将继续循环。
Your solution of changing to AND is correct - you want the value to not be A
andnot be B
(i.e. it's neither A norB).
你要改变和解决方案是正确的-你想要的值不A
和不B
(即它既不是也不是B)。
Alternatively, you could use OR internally, but put a NOT around the whole thing:
或者,您可以在内部使用 OR,但在整个事情周围放置一个 NOT:
while (! (temp.equalsIgnoredCase("A") || temp.equalsIgnoreCase("B") || ...))
回答by Rohit Jain
I still don't understand why OR doesn't work but AND does
我仍然不明白为什么 OR 不起作用但 AND 起作用
The expression using ||
will always be trueat any given value of temp. Because, tempcannot be both a
and b
at the same time. If it is a
, then the 2nd part of ||
will be true, and if it is equal to b
or any other value, the first part will be true, thus making the entire expression truein both the cases.
在temp 的任何给定值下,using 的表达式||
将始终为真。因为,温度不能同时并在同一时间。如果是,则第二部分为真,如果等于或任何其他值,第一部分将为真,从而使整个表达式在两种情况下都为真。a
b
a
||
b
With &&
, your while
will only be true
, if tempis neither of a
nor b
.
与&&
,你while
只会是true
,如果temp既不是a
也不是b
。
Alternatively, if you are going to test tempagainst many values, you can change your while
condition to look simpler:
或者,如果您要针对多个值测试temp,您可以将while
条件更改为看起来更简单:
while (!"ABDPS".contains(temp.toUpperCase())) {
}
回答by Ankit
its a foul logic. the code
这是一个犯规的逻辑。代码
(!temp.equalsIgnoreCase("A") || !(temp.equalsIgnoreCase("B")) ||!temp.equalsIgnoreCase("D")|| !temp.equalsIgnoreCase("P") || !temp.equalsIgnoreCase("S"))
means
方法
if char is not A, or not B, or not D, or not P, or not S. It will always evaluate to true, since is char is A, it will neither be B,D,S nor P. so is for the others.
如果char不是A,或者不是B,或者不是D,或者不是P,或者不是S。它总是评估为真,因为char是A,它既不是B,D,S也不是P。所以是其他。
if you want it to be OR logic, it should be:
如果你希望它是 OR 逻辑,它应该是:
(!(temp.equalsIgnoreCase("A") || (temp.equalsIgnoreCase("B")) ||temp.equalsIgnoreCase("D")|| temp.equalsIgnoreCase("P") || temp.equalsIgnoreCase("S")))
which means, not when the char is either of A, B, D,S or P
这意味着,不是当字符是 A、B、D、S 或 P 时
回答by Sorontur
This is all about logic.
这都是关于逻辑的。
A OR B means that is is true when A is true or B is true or both are true.
A OR B 表示当 A 为真或 B 为真或两者都为真时为真。
In your special case it is only possible that one of your equalsIgnorecase() can ever work, so you wrote something like a tautology which means an endless loop. You can read about boole algebra here: http://en.wikipedia.org/wiki/Boolean_algebra_%28structure%29
在您的特殊情况下,您的 equalsIgnorecase() 中的一个可能永远有效,因此您编写了类似重言式的内容,这意味着无限循环。你可以在这里阅读布尔代数:http: //en.wikipedia.org/wiki/Boolean_algebra_%28structure%29
Kind of some theory but it explains what you need to know when you write boolean expressions. Hope this helps :)
有点理论,但它解释了编写布尔表达式时需要知道的内容。希望这可以帮助 :)