日期:2014-05-17 浏览次数:21206 次
List<int> list = new List<int>() { 1, 3, 10, 6, 4, 2, 15, 1, 10, 8, 9, 20, 4, 11, 3 }; foreach (int i in list) { if (i > 10) { Console.WriteLine("{0} larger than 10! Loops are breaked.", i); break; } else { Console.WriteLine(i); } }
------解决方案--------------------
同问啊
C#怎么在执行完一句判断语句后,在循环内将其移除?
------解决方案--------------------
有一种思路,可以改用for来实现:
List<int> list = new List<int>() { 1, 2, 3, 4, 5 }; for (int i = 0; i < list.Count; i++) { if (list[i] == 3) { for (; i < list.Count; i++) Console.WriteLine(list[i]); break; } }
------解决方案--------------------
改成“大于等于3”比较恰当点:
List<int> list = new List<int>() { 1, 2, 3, 4, 5 }; for (int i = 0; i < list.Count; i++) { if (list[i] >= 3) { for (; i < list.Count; i++) Console.WriteLine(list[i]); break; } }