Java do while loop making string testing act differently

后端 未结 3 1251
北荒
北荒 2020-12-20 10:34

I am very confused at the behavior of using the || operator on the .equals function. Is there a reason I can not use it on strings or something?

this works:

<
3条回答
  •  遥遥无期
    2020-12-20 11:21

    Let's put it into words.

    "Eat all of this fruit as long as it's not an apple OR it's not an orange."

    • Strawberry: not an apple, continue.
    • Banana: not an apple, continue.
    • Orange: not an apple, so... continue.
    • Apple: is apple; but is actually not an orange, so... continue...... :(

    If it's an apple, "not an orange" will be true; if it's an orange, "not an apple" will be true; if it's a kiwi, both will be true. There is no way to stop eating (unless you explode or crash into a coma).

    Bad logic leads people to death by fruit.

    You want "eat all of this fruit as long as it's not an apple AND ALSO not an orange".

提交回复
热议问题