gpt4 book ai didi

c# 重新启动 for 循环

转载 作者:太空狗 更新时间:2023-10-29 20:47:40 28 4
gpt4 key购买 nike

所以我有这几行代码:

string[] newData = File.ReadAllLines(fileName)
int length = newData.Length;
for (int i = 0; i < length; i++)
{
if (Condition)
{
//do something with the first line
}
else
{
//restart the for loop BUT skip first line and start reading from the second
}
}

我试过使用 goto,但如您所见,如果我再次启动 for 循环,它将从第一行开始。

那么如何重新启动循环并更改起始行(从数组中获取不同的键)?

最佳答案

我认为 for 循环 在这里是错误的循环类型,它没有正确表达循环的意图,并且肯定会向我建议你不去弄乱柜台。

int i = 0;
while(i < newData.Length)
{
if (//Condition)
{
//do something with the first line
i++;
}
else
{
i = 1;
}
}

关于c# 重新启动 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10787598/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com