简体   繁体   English

C#如何摆脱嵌套的for循环并继续新的循环?

[英]C# How to break out of a nested for loop and continue with a new loop?

I am making a quest system for my game in Unity2D. 我正在为Unity2D中的游戏制作一个任务系统。 I stored all my different kind of quests inside a dictionary but only 5 quests to be picked randomly for the player. 我将所有不同类型的任务存储在字典中,但只有5个任务可供玩家随机选择。 I used nested loops first to get 5 random numbers, and then another foreach loop to access the elements inside the dictionary and lastly planned to use another for loop to print out all the picked quests. 我首先使用嵌套循环获得5个随机数,然后使用另一个foreach循环访问字典中的元素,最后计划使用另一个for循环打印出所有选择的任务。 However, I am having problem because it printed out 25times, instead of 5. Is there a way to break out of the 2nd loop and go into the 3rd loop? 但是,我遇到了问题,因为它打印了25次而不是5次。是否有办法摆脱第二个循环并进入第三个循环? Thanks! 谢谢!

void Start()
{
    // missions
    missions.Add("Visit Mural Stops x ", 1);
    missions.Add("Collect Cards x ", 2);
    missions.Add("Fight Monsters x", 3);
    missions.Add("Collect Coins x ", 4);
    missions.Add("Collect Items x", 5);
    missions.Add("Fight SBM Boss x ", 6);
    missions.Add("Find Mural Stops  x", 7);

    var numbers = new List<int>(missions.Count);
    for (int i = 0; i < missions.Count; i++)
    {
        numbers.Add(i);
    }

    var randomNumbers = new int[text.Length];
    // text.Length is always 5, as I only have 5 mission texts

    for (int i = 0; i < randomNumbers.Length; i++)
    {
        var thisNumber = Random.Range(1, numbers.Count);
        randomNumbers[i] = numbers[thisNumber];
        numbers.RemoveAt(thisNumber);
        //Debug.Log("random " + randomNumbers[i]);

        foreach (KeyValuePair<string, int> pairs in missions)
        {
            if (pairs.Value == randomNumbers[i])
            {
                //Debug.Log(pairs.Key); // print out 5 missions

                missionText = pairs.Key;

                for (int j = 0; j < text.Length; j++)
                {
                    text[j].text = missionText; // it only print out the last picked quests
                    Debug.Log(missionText); // each picked quest is printed out 5 times instead of once
                    // how do i print out only 5 times inside this loop?
                }
            }
        }
    }
}

Your problem is your last loop: 您的问题是最后一个循环:

for (int j = 0; j < text.Length; j++)
{
    text[j].text = missionText;
    Debug.Log(missionText);
}

This is running the Debug.Log five times - once for each value of j (and you say earlier that text.Length is always 5). 这将运行Debug.Log五次-对于j的每个值一次(您之前说过text.Length始终为5)。

In comments you say want to print all the picked quests into my texts. 在评论中,您说要将所有选择的任务打印到我的文本中。

You already have an index for your text - i - since your randomNumbers array is the same length as your texts and indeed the whole point of randomNumbers seems to be to generate a random number for each entry of text . 因为您的randomNumbers数组的长度与文本的长度相同,所以您已经有了文本的索引i ,实际上randomNumbers的全部要点似乎是为每个text条目生成一个随机数。 So rather than that loop I quoted what you want to do is: 因此,我引用的不是您要执行的循环:

text[i].text = missionText;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM