[英]How to Break a For Loop From an If Statement Inside the Loop (C#)
I am working on an A* Pathfinding method that uses a custom class instead of nodes, but am having issues with my loops.我正在研究使用自定义类而不是节点的 A* 寻路方法,但我的循环存在问题。 The first for loop using int i is able to go up to 3 (Player1.instance.movement = 3), but I need to use an if statement inside of that loop to check if the target position has already been found.
使用 int i 的第一个 for 循环最多可以达到 3(Player1.instance.movement = 3),但我需要在该循环内使用 if 语句来检查是否已经找到目标位置。 I am wondering if it is possible to break my for loop when my If statement is false.
我想知道当我的 If 语句为假时是否有可能打破我的 for 循环。 public void GetNeighbors(Tile originTile) { Tile originalTile = originTile;
public void GetNeighbors(Tile originTile) { Tile originalTile = originTile;
nextTile.Clear();
int minX = 0;
int minY = 0;
var originCostFunc = Mathf.Infinity;
for (int i = 0; i < Player1.instance.movement; i++)
{
for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
if (x != y && y != x)
{
var costX = Mathf.Abs((originTile.transform.position.x + x) - originalTile.transform.position.x);
var costY = Mathf.Abs((originTile.transform.position.y + y) - originalTile.transform.position.y);
var distanceX = Mathf.Abs(targetPos.transform.position.x - (originTile.transform.position.x + x));
var distanceY = Mathf.Abs(targetPos.transform.position.y - (originTile.transform.position.y + y));
var costFunc = costX + costY + distanceX + distanceY;
if (costFunc <= originCostFunc)
{
originCostFunc = costFunc;
minX = x;
minY = y;
Debug.Log($"x: {x}, y: {y}");
}
}
}
}
nextTile.Add(GridManagerHandPlaced.instance.GetTileAtPosition(new Vector2(originTile.transform.position.x + minX, originTile.transform.position.y + minY)));
if (nextTile[i] != targetPos)
{
originTile = nextTile[i];
}
else
{
break;
}
}
DisplayPath();
}
You can break loop several times by condition.您可以按条件多次中断循环。
bool breakLoop = false;
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
for (int k = 0; k < length; k++)
{
breakLoop = nextTile == target;
if (breakLoop)
break;
}
if (breakLoop)
break;
}
if (breakLoop)
break;
}
Or move search logic to separated method and return a value from any number of nested loops或者将搜索逻辑移动到分离的方法并从任意数量的嵌套循环中返回一个值
string path = FindPath();
Display(path);
string FindPath()
{
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
for (int k = 0; k < length; k++)
{
if (nextTile == target)
return nextTile;
}
}
}
return null;
}
Never use goto operator.永远不要使用 goto 运算符。
This is one of the few valid cases where I'd use goto
.这是我使用
goto
的少数有效案例之一。 In-fact, this is the example given in the docs for when it should be used.事实上,这是文档中给出的示例,说明何时应该使用它。
void CheckMatrices(Dictionary<string, int[][]> matrixLookup, int target)
{
foreach (var (key, matrix) in matrixLookup)
{
for (int row = 0; row < matrix.Length; row++)
{
for (int col = 0; col < matrix[row].Length; col++)
{
if (matrix[row][col] == target)
{
goto Found;
}
}
}
Console.WriteLine($"Not found {target} in matrix {key}.");
continue;
Found:
Console.WriteLine($"Found {target} in matrix {key}.");
}
}
Note the syntax for the label is simply myLabel:
and you can place it anywhere in procedurally executable code.请注意,标签的语法就是
myLabel:
您可以将它放在程序可执行代码中的任何位置。
For sake of covering other ways of handling this situation, here is the boolean solution.为了涵盖处理这种情况的其他方式,这里是布尔解决方案。
bool breakLoops = false;
for (int i = 0; i < length1; i++)
{
for (int ii = 0; ii < length2; ii++)
{
for (int iii = 0; iii < length3; iii++)
{
if (breakingCondition)
{
breakLoops = true;
break;
}
}
if (breakLoops) break;
}
if (breakLoops) break;
}
Simple and straightforward, but requires a break condition check at the end of each loop that you want to break out of.简单明了,但需要在要中断的每个循环结束时进行中断条件检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.