剛學習程序,感覺寫代碼 很有意思,所以把自己的感悟寫下來啦,第一次寫博客,可能是菜鳥中的菜鳥 時間久了,相信就會寫的很好哦!
for和 foreach 的數組遍歷 比較
很簡單的程序,不解釋啦!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] nums = { 3, 5, 99, 23, 53, 88 }; //定義數組
int max = 0;
for (int i = 0; i < nums.Length; i++) //for遍歷數組
{
if (nums[i] > max)
{
max = nums[i];
}
}
Console.WriteLine("for遍歷數組demo:" + max);
Console.WriteLine("====================================================");
foreach (var p in nums) //使用foreach 遍歷
{
if (p > max)
{
max = p;
}
}
Console.WriteLine("foreach遍歷數組demo:"+max);
}
}
}
結果為:

