[英]How to continue from the break in a for loop
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;
}
}
Is it possible to continue the inner loop from where it stops so I can get the output of:是否可以从停止的位置继续内部循环,以便我可以获得以下输出:
I like C++.
I like C#.
I like Java.
做这个:
richTextBox1.Text = string.Join("\n", sentence );
The break
keyword will always "break-out" of the current block of code. break
关键字将始终“突破”当前代码块。 In this case, it is your inner for-loop.在这种情况下,它是您的内部 for 循环。
I think you looking for the continue
keyword.我认为您正在寻找
continue
关键字。
Try simply below:简单地在下面尝试:
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";
}
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;
}
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;
}
}
Instead of int j = 0, make it as int j = i in the second loop.. Though there are better solutions to "THIS" problem, I assume you are looking at a different set of problems to check if such continuation is possible.而不是 int j = 0,在第二个循环中将其设置为 int j = i .. 虽然有更好的解决“这个”问题的方法,但我假设您正在查看一组不同的问题来检查这种延续是否可能。
Maybe you do not want to end of the line after the last item?也许您不想在最后一项之后结束行? Then:
然后:
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];
if(i < sentence.Length-1)
richTextBox1.Text += "\n";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.