标签:
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();
}
标签:
原文地址:http://www.cnblogs.com/handsome1008/p/4659032.html