在C#中,使用while循环遍历数组的方法如下:
int[] numbers = { 1, 2, 3, 4, 5 };
i
,并将其设置为0。这将用于访问数组中的每个元素:int i = 0;
while (i< numbers.Length)
{
Console.WriteLine(numbers[i]);
i++; // 更新索引变量,以便在下次迭代中访问下一个元素
}
完整的示例代码如下:
using System;
class Program
{
static void Main()
{
int[] numbers = { 1, 2, 3, 4, 5 };
int i = 0;
while (i< numbers.Length)
{
Console.WriteLine(numbers[i]);
i++;
}
}
}
运行此程序后,控制台将输出数组中的所有元素。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:如何在php中使用for循环遍历数组