如何重置或重新启动嵌套循环

发布于 2024-10-06 00:32:54 字数 541 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倾`听者〃 2024-10-13 00:32:54

您必须使用 goto 来打破 C# 中的多个循环级别。例如:

RESTART:
    while (a) {
        while (b) {
            if (that_other_thing)
                goto RESTART;
        }
    }

嗯,您不必使用 goto,但替代方案可能是使用一堆标志变量来指示需要重新启动。而且该代码可能很难遵循。

You have to use goto to break out of multiple loop levels in C#. For example:

RESTART:
    while (a) {
        while (b) {
            if (that_other_thing)
                goto RESTART;
        }
    }

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.

南风起 2024-10-13 00:32:54

这里最好的选择是将循环移动到它们自己的方法中,并从内部循环内部返回。示例:

public void MyMehod(){
  loop one{
    looptwo{
      if(condition=true){
        return;
      }
    }
  }
}

如果由于某种原因这是不可能的,您可以使用在内部循环中设置的 bool 值来摆脱所有这些,但这有点混乱:

bool endloop = false;
while(!endloop){
  while(!endloop){
    if(condition){
      endloop = true;
    }
  }
}

对于 while 循环,它看起来没问题,但甚至对于 for 循环或 foreach 循环来说更加混乱。

The best choice here is to move the loops into their own method, and return from inside the inner loop. Example:

public void MyMehod(){
  loop one{
    looptwo{
      if(condition=true){
        return;
      }
    }
  }
}

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:

bool endloop = false;
while(!endloop){
  while(!endloop){
    if(condition){
      endloop = true;
    }
  }
}

For a while loop it looks ok, but even more messy for a for loop or a foreach loop.

小矜持 2024-10-13 00:32:54
            Start:
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    if(j == 5)
                        goto Start;
                }
            }

虽然以不使用 goto 的方式构建代码是一个更好的方法......

            Start:
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    if(j == 5)
                        goto Start;
                }
            }

Although structuring your code in a way to not use a goto is a much better approach...

飞烟轻若梦 2024-10-13 00:32:54

如果您可以保证有一个条件告诉您不需要重新启动,那么您可以将整个事情包装在一个循环中。

bool keepLooping = true;
while (keepLooping)
{
  keepLooping = false;
  for (int x = 0; x < maxx; x++)
  {
    for (int y = 0; y < maxy; y++)
    {
      if (DoSomething(x, y))
      {
        keepLooping = true;
        break;
      }
    }
    if (keepLooping)
    {
      break;
    }
  }
}

如果您正在检查列表中的重复项并修改它们确实使所有条目都唯一,您可能会执行以下操作(假设字符串值):

List<string> a = GetNamesFromeSomewhere();

bool duplicateFound = true;
while (duplicateFound )
{
  duplicateFound = false;
  for (int x = 0; x < a.Length; x++)
  {
    for (int y = x + 1; y < a.Length; y++)
    {
      if (a[x].Equals(a[y]))
      {
        //Change a[y], but now we have to recheck for duplicates...
        a[y] += "_";
        duplicateFound = true;
        break;
      }
    }
    if (duplicateFound)
    {
      break;
    }
  }
}

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.

bool keepLooping = true;
while (keepLooping)
{
  keepLooping = false;
  for (int x = 0; x < maxx; x++)
  {
    for (int y = 0; y < maxy; y++)
    {
      if (DoSomething(x, y))
      {
        keepLooping = true;
        break;
      }
    }
    if (keepLooping)
    {
      break;
    }
  }
}

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):

List<string> a = GetNamesFromeSomewhere();

bool duplicateFound = true;
while (duplicateFound )
{
  duplicateFound = false;
  for (int x = 0; x < a.Length; x++)
  {
    for (int y = x + 1; y < a.Length; y++)
    {
      if (a[x].Equals(a[y]))
      {
        //Change a[y], but now we have to recheck for duplicates...
        a[y] += "_";
        duplicateFound = true;
        break;
      }
    }
    if (duplicateFound)
    {
      break;
    }
  }
}
愿与i 2024-10-13 00:32:54

如果您使用 i 和 j 等数字循环变量,您只需重置值即可,

例如

for (i=0; i<10; i++) {
   for (j=0; j<10; j++) {
      if (var[i][j] == 'x') {
         i=0; j=0; break;
      }
   }
}

您还可以使用前面建议的方法

void someFunction(params) {
   for (i=0; i<10; i++) {
      for (j=0; j<10; j++) {
         if (var[i][j] == 'x') {
            someFunction(params)
            return;
         }
      }
   }       
}

if you use numeric loop variables like i and j you can just reset the values

e.g.

for (i=0; i<10; i++) {
   for (j=0; j<10; j++) {
      if (var[i][j] == 'x') {
         i=0; j=0; break;
      }
   }
}

you can also use the method approach as suggested earlier

void someFunction(params) {
   for (i=0; i<10; i++) {
      for (j=0; j<10; j++) {
         if (var[i][j] == 'x') {
            someFunction(params)
            return;
         }
      }
   }       
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文