Java do while loop making string testing act differently

后端 未结 3 1270
北荒
北荒 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:12

    || means OR.

    A || B is true if A is true or B is true.

    In your second while loop: !"Y".equals(input) is false because Y is equal to your input. However !"N".equals(input) is true because N is not equal to your input. Hense the whole condition is true, it goes into while loop again.

提交回复
热议问题