【问题标题】:Moving backwards in a FORWARD for loop在 FORWARD for 循环中向后移动
【发布时间】:2020-12-19 21:58:15
【问题描述】:

我不确定如何解释这一点,也无法在任何地方找到答案。我有一个 for 循环,它遍历通过字符串表示的行。

for (int i = 0; i < dataLines.Length; i++)
{

    jumpPoint:
        Debug.Log("Jumped");

    string[] words = dataLines[i].Split();
    .
    .               
    . "words[] manipulation and reading"
    .
    .
}

我的任何数据处理或循环中发生的事情都没有问题。 但是我有一个实例,我需要转到以前的 dataLine[] 并从该点继续(也重新运行自该点以来已经运行的代码)。

我所做的基本上归结为

i = ?; //arbitrary number for the situation that is definitely not out of bounds for the loop
goto jumpPoint;

我也尝试过不使用跳转点,也只是在重置 for 循环索引后让循环循环到下一个。

我知道跳转点不是问题,因为它用于无关的事情,并且跳转工作正常。还值得一提的是,在这些情况下,我正在增加 i 索引,因此 for 循环过早推进并且效果很好。

那么为什么我不能在循环中倒退呢?这只是不可能的事情吗?

【问题讨论】:

  • 这听起来像是一个糟糕的设计。您所描述的是邀请无限循环发生。换句话说,如果您需要循环回到较低的索引,那么听起来您需要一种不同类型的循环,比如可能是while 循环。不知道你想要达到的“什么”很难说。
  • @JohnG 我应该澄清一下。我正在为我正在制作的游戏编写一种简单的伪语言。如果用户编写的程序会导致这种情况,我希望发生无限循环的可能性。该代码位于其自己的小区域中,并带有检查,因此可以在维护整个程序的同时关闭无限运行的代码(我已经以不同的风格实现了)。我的问题需要的是伪语言中的“goto”类型语句。
  • 如果那是不可行的,或者只是行不通,我会废弃它的那一部分,因为它在宏伟计划中并不是完全必要的。

标签: c# loops for-loop unity3d


【解决方案1】:

不确定我是否理解,但您的意思是这样的:

        var i = 0;
        var startRun = true;

        while (true)
        {
            if (!startRun && i == dataLines.Length - 1)
                return;

            if (startRun)
                startRun = false;

            /* do stuff */

            if (needToJump)
            {
                clearLogicOrWhatever();
                i = whereYouNeedToJump;
            }
            else
                i++;
        }

当然,如果超出索引,如果步骤存在或在范围内和其他东西,您将需要更多验证,这就像概念证明/伪。

【讨论】:

    【解决方案2】:

    没有什么能阻止你在循环中倒退。您可以控制您的代码逻辑,因此您只需在满足“jumpPoint”条件时,将迭代器 i 更改回您想要的前一个值。

    例如:假设我想跳回并重新运行某个内容,因为其中包含“跳转”一词。

    Console.WriteLine("App started.");
    List<string> dataLines = new List<string>() { "this is a phrase", "This is another but with jump in it", "this is the last" };
    bool alreadyJumped = false;
    for (int i = 0; i < dataLines.Count; i++)
    {
        Console.WriteLine($"Currently Iterating: {i}");
        string[] words = dataLines[i].Split();
        Console.WriteLine($"Do some with this data: {dataLines[i]}");
        if (words.Contains("jump") && !alreadyJumped)
        {
            alreadyJumped = true;
    
            // Reset the i value so that the next iteration will run again.
            i = i - 1;
        }
        else if (alreadyJumped)
        {
            // Once 
            alreadyJumped = false;
        }
    
    }
    Console.WriteLine("App done.");
    

    这将产生这个输出:

    App started.
    Currently Iterating: 0
    Do some with this data: this is a phrase
    Currently Iterating: 1
    Do some with this data: This is another but with jump in it
    Currently Iterating: 1
    Do some with this data: This is another but with jump in it
    Currently Iterating: 2
    Do some with this data: this is the last
    App done.
    

    【讨论】:

    • 感谢您的回复。当尝试以我上面展示的方式或与您的评论类似的其他方式时,程序崩溃了,我只是不明白为什么。我没有调试输出或任何可以关闭的东西,这让我觉得这是不可能的,但我不确定。
    • 不幸的是,当您“跳转”时,您在代码中所做的事情有很大的空白,我们无法看到或理解,除非您向我们提供最小的可重现示例。不确定您的问题是什么,但我上面提供的代码已编译并运行以产生我发布的输出,所以我知道可以在 for 循环中随意跳转。
    • 是的,我想我有一些事情正在发生,导致只有在尝试执行此操作时才会出现问题。这个文件是 1000 行,所以我会搜索直到我弄明白,感谢您的帮助。这让我意识到这可能是别的东西。
    • 是的,我同意。听起来像别的东西。祝你好运!
    猜你喜欢
    • 2023-03-13
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 2022-06-13
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多