C#遍历数组

在C#中,可以使用foreach语句来遍历数组的元素:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            // C#遍历数组-www.baike369.com
            int odd = 0; // 奇数
            int eve = 0; // 偶数
            int[] array = new int[100];
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = i*5;
            }
            foreach (int j in array)
            {
                if (j % 2 == 0)
                    eve++;
                else
                    odd++;
            }
            Console.WriteLine("奇数有" + odd + "个");
            Console.WriteLine("偶数有" + eve + "个");
            Console.ReadLine();
        }
    }
}

运行结果:
 
奇数有50个
偶数有50个

 

posted @ 2015-01-22 14:52  melao2006  阅读(4105)  评论(0编辑  收藏  举报