如何在for循环中继续休息

时间:2015-04-22 14:21:11

标签: c# loops

string[] sentence = new string[] { "I like C++.", "I like C#.", "I like Java." };

for (int i = 0; i < sentence.Length; i++)
{
    for (int j = 0; j < sentence.Length; j++)
    {
        richTextBox1.Text += sentence[j] + "\n";
        break;
    }
}

是否可以从停止的地方继续内循环,这样我就可以获得输出:

I like C++.
I like C#.
I like Java.

5 个答案:

答案 0 :(得分:1)

这样做:

richTextBox1.Text = string.Join("\n", sentence );

答案 1 :(得分:1)

break关键字将始终“突破”当前代码块。在这种情况下,它是你的内部循环。

我认为您正在寻找continue关键字。

答案 2 :(得分:0)

在下面试试:

string[] sentence = new string[] { "I like C++.", "I like C#.", "I like Java." };

for (int i = 0; i < sentence.Length; i++)
{
        richTextBox1.Text += sentence[j] + "\n";
}

答案 3 :(得分:0)

string[] sentence = new string[] { "I like C++.", "I like C#.", "I like Java." };

            for (int i = 0; i < sentence.Length; i++)
            {
                xyz:

                for (int j = i; j < sentence.Length; j++)
                {
                    richTextBox1.Text += sentence[j] + "\n";
                    break;
                }

                 i++;
                 if (i == sentence.Length) { break; }
                 goto xyz;
            }

答案 4 :(得分:0)

string[] sentence = new string[] { "I like C++.", "I like C#.", "I like Java." };

for (int i = 0; i < sentence.Length; i++)
{
    for (int j = **i**; j < sentence.Length; j++)
    {
        richTextBox1.Text += sentence[j] + "\n";
        break;
    }
}

而不是int j = 0,在第二个循环中将其设为int j = i ..虽然有更好的解决方案来解决“这个”问题,但我假设您正在查看一组不同的问题来检查是否存在这样的延续是可能的。