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+"个"); } }
时间: 2024-10-24 04:58:01