首页 > 代码库 > foreach & Random

foreach & Random

using System;public class ForEachTest{    public static void Main()    {        int odd=0;        int eve=0;        int length=100;        int[] arr=new int[length];        Random r = new Random();        for(int i=0;i<length;i++)  //随机数赋值        {            arr[i]= r.Next();        }            foreach(int tem in arr)  //foreach 举例        {            if(tem%2==0)                eve++;            else                odd++;        }        Console.WriteLine("奇数"+odd+"");        Console.WriteLine("偶数"+eve+"");    }    }

 

foreach & Random