1、while循環
static void Main(string[] args)
{
int[] hs = { 1,2,3,4,5,6,7,8,9};
int ligh = hs.Length;
while (ligh > 0)
{
Console.WriteLine(hs[ligh - 1]);
ligh -= 1;
}
Console.ReadKey();
}
2、for循環(可以嵌套for循環,比如:做冒泡排序的時候會用到)
static void Main(string[] args)
{
int[] hs = { 1,2,3,4,5,6,7,8,9};
//倒敘打印只需要修改一下判斷條件即可
for (int i = 0; i < hs.Length; i++)
{
Console.WriteLine(hs[i].ToString());
}
Console.ReadKey();
}
3、foreach循環遍歷集合中的元素(這種寫法貌似是.NET獨有的吧)
static void Main(string[] args)
{
int[] hs = { 1,2,3,4,5,6,7,8,9};
//這裡用到了var關鍵字,匿名類型(由編譯器自動推斷),你可以把它換成int
foreach (var item in hs)
{
Console.WriteLine(item.ToString());
}
Console.ReadKey();
}