剛學(xué)習(xí)程序,感覺寫代碼 很有意思,所以把自己的感悟?qū)懴聛砝玻谝淮螌懖┛停赡苁遣锁B中的菜鳥 時(shí)間久了,相信就會(huì)寫的很好哦!
for和 foreach 的數(shù)組遍歷 比較
很簡單的程序,不解釋啦!
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 }; //定義數(shù)組 int max = 0; for (int i = 0; i < nums.Length; i++) //for遍歷數(shù)組 { if (nums[i] > max) { max = nums[i]; } } Console.WriteLine("for遍歷數(shù)組demo:" + max); Console.WriteLine("===================================================="); foreach (var p in nums) //使用foreach 遍歷 { if (p > max) { max = p; } } Console.WriteLine("foreach遍歷數(shù)組demo:"+max); } }}
結(jié)果為:
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注