如何重置或重新启动嵌套循环
loop one
{
looptwo
{
if(condition=true)
{
reset values//restart both loops
}
}
}
and possibilities for reset values is 3
basically i want to compair two matrices
a= 1 2 3 4
1 2 3 4
b= 3 4 5 6
4 6 7 8
and when row 1 of a[] is matched with row 1 of b[].....i will add these rows and a[]
become = 2 4 6 8
for(i=0;i<rows;i++)
for(j=0;j<columns;j++)
{
a[i]=a[i]+b[i,j]
}
并再次使用新的 a[] 矩阵重新启动找到我的 maches
,并且我必须确保 b[] 矩阵的所有行都用 a[] 检查,在本例中为 3
loop one
{
looptwo
{
if(condition=true)
{
reset values//restart both loops
}
}
}
and possibilities for reset values is 3
basically i want to compair two matrices
a= 1 2 3 4
1 2 3 4
b= 3 4 5 6
4 6 7 8
and when row 1 of a[] is matched with row 1 of b[].....i will add these rows and a[]
become = 2 4 6 8
for(i=0;i<rows;i++)
for(j=0;j<columns;j++)
{
a[i]=a[i]+b[i,j]
}
and again find my maches from restart with new a[] Matrix
and i have to insure that all rows of b[] matrix are checked with a[] which are 3 in this case
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您必须使用
goto
来打破 C# 中的多个循环级别。例如:嗯,您不必使用 goto,但替代方案可能是使用一堆标志变量来指示需要重新启动。而且该代码可能很难遵循。
You have to use
goto
to break out of multiple loop levels in C#. For example:Well, you don't have to use
goto
but the alternative might be using a bunch of flag variables to indicate that a restart is required. And that code will probably be pretty hard to follow.这里最好的选择是将循环移动到它们自己的方法中,并从内部循环内部返回。示例:
如果由于某种原因这是不可能的,您可以使用在内部循环中设置的 bool 值来摆脱所有这些,但这有点混乱:
对于 while 循环,它看起来没问题,但甚至对于
for
循环或foreach
循环来说更加混乱。The best choice here is to move the loops into their own method, and return from inside the inner loop. Example:
If this is not possible for some reason, you can use a bool value that you set in the inner loop to bail out of all of them, but this is a bit more messy:
For a while loop it looks ok, but even more messy for a
for
loop or aforeach
loop.虽然以不使用
goto
的方式构建代码是一个更好的方法......Although structuring your code in a way to not use a
goto
is a much better approach...如果您可以保证有一个条件告诉您不需要重新启动,那么您可以将整个事情包装在一个循环中。
如果您正在检查列表中的重复项并修改它们确实使所有条目都唯一,您可能会执行以下操作(假设字符串值):
If you can guarantee that you will have a condition that will tell you that you don't need to restart, you could wrap the whole thing in one more loop.
If you are checking a list for duplicates and modifying them do make all entries unique, you might do something like this (assuming string values):
如果您使用 i 和 j 等数字循环变量,您只需重置值即可,
例如
您还可以使用前面建议的方法
if you use numeric loop variables like i and j you can just reset the values
e.g.
you can also use the method approach as suggested earlier