大家好,你怎么能在 foreach 中重复一次迭代?
foreach (string line in File.ReadLines("file.txt"))
{
// now line == "account", next line == "account1"
if (line.Contains("a"))
//next loop take "account1";
else
// need to set that next loop will take line == "account" again
}
Run Code Online (Sandbox Code Playgroud)
怎么做?
小智 8
虽然我不完全理解你的例子,但我想我理解你的问题。我遇到了同样的问题,并且能够想出一个解决方案:在 foreach 中包含一个 while 循环。在您的示例中,它看起来像这样:
foreach (string line in File.ReadLines("file.txt"))
{
bool repeat = true;
while (repeat)
{
// now line == "account", next line == "account1"
if (line.Contains("a"))
{
//do your logic for a break-out case
repeat = false;
}
else
{
//do your logic for a repeat case on the same foreach element
//in this instance you'll need to add an "a" to the line at some point, to avoid an infinite loop.
}
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我玩游戏已经很晚了,但希望这对遇到同样问题的其他人有所帮助。
| 归档时间: |
|
| 查看次数: |
6709 次 |
| 最近记录: |