Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何缩短这个嵌套的 if 语句?
if(x > 0){ if(grid[x-pixelOffset,y] == true){ middleLeft = 1; } }
您可以使用&&运算符:
&&
if ((x > 0) && grid[x-pixelOffset,y]) ...
== true检查bool变量值时不需要。
== true
bool
只是为了获取更多信息,如果不需要短路,您可以使用 &。如果你写 if( (x>0) & grid[x,y]) {...},第二部分也将被评估。