Loops 我如何跳过检查我的职位 for(int x=0;x
假设当前位置的变量名为Loops 我如何跳过检查我的职位 for(int x=0;x,loops,for-loop,Loops,For Loop,假设当前位置的变量名为current\u x和current\u y,那么您可以在循环中执行类似操作 for (int x = 0; x <= battleField.getCols(); x++){ for (int y = 0; y <= battleField.getRows(); y++){ if ((battleField.get(x,y) == team) && ((x +
current\u x
和current\u y
,那么您可以在循环中执行类似操作
for (int x = 0; x <= battleField.getCols(); x++){
for (int y = 0; y <= battleField.getRows(); y++){
if ((battleField.get(x,y) == team) && ((x + y)
!= battleField.get(row, col)))
{
.
.
.
}
}
}
而不是
bool isMyPosition = (x == current_x) && (y == current_y);
if ( !isMyPosition
&& (battleField.get(x,y) == team)
&& ((x + y) != battleField.get(row, col)))
{
...
}
我想你想要
((x + y) != battleField.get(row, col))
如果您确实想要第一部分,您可以将其添加到内部for循环的开头:
(x != row && y != col)
for(int x=0;xif(Question.isbaout(EscapingOnCheck))
大多数语言都有某种类型的continue
,可用于在满足条件时跳出循环
否则
将当前位置保存在某些变量中,并添加另一个嵌套循环(为了调试)或添加一个&&子句(为了减少代码)为什么X+Y应该是唯一的?对于每个(行,列),都会有一个(列,行)应该给出相同的值。您能详细说明一下吗?get(X,Y)你也是对的,但我只是想用一个更简单的方法来解决我的问题。谢谢
for (int x = 0; x <= battleField.getCols(); x++){
for (int y = 0; y <= battleField.getRows(); y++){
if(x == row && y == col) // This will check if the loop is looking at your position
continue; // This will go to the next step in the "y" for loop, essentially skipping the rest of the code in this block
if ((battleField.get(x,y) == team) && ((x + y)
!= battleField.get(row, col)))
{
.
.
.
}
}