!=运算符不能正常工作

1 c#

我目前正试图让我的蛇控制台游戏运行,但我想出了一些我不太了解的东西.我真的不认为!=操作员工作不正常,所以我一定犯了一个错误,但我不知道为什么会这样:

 // not working
 if (food.x != snakeElements.Last().x && food.y != snakeElements.Last().y) 

 // working
 if (!(food.x == snakeElements.Last().x && food.y == snakeElements.Last().y)) 
Run Code Online (Sandbox Code Playgroud)

不一样吗?

Lee*_*Lee 21

使用De Morgan的定律 (!a && !b)!(a || b)第一个例子应该是一样的:

if (food.x != snakeElements.Last().x || food.y != snakeElements.Last().y)
Run Code Online (Sandbox Code Playgroud)


Maa*_*ten 5

&&应该是|| 在第一个如果.

if (food.x != snakeElements.Last().x || food.y != snakeElements.Last().y) 
Run Code Online (Sandbox Code Playgroud)