using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
List<int> lt = new List<int>();//容器
List<int> lt1 = new List<int>();//偶数容器
List<int> lt2 = new List<int>();//奇数容器
int opp = 0;//偶数个数
int od = 0;//奇数个数
string str;//输入十个整数
Console.WriteLine("请输入十个整数:");
while ((str = Console.ReadLine())!=null)
{
try
{
string[] strtemp = str.Split(' ');//分解
foreach (string str1 in strtemp)
{
lt.Add(int.Parse(str1));//加入容器
}
foreach (int n in lt)
{
if (n % 2 == 0)
{
opp++;
lt1.Add(n);//加入偶容器
}
else
{
od++;
lt2.Add(n);//加入寄容器
}
}
Console.Write("偶数个数:{0};奇数个数:{1}\n", opp, od);
Console.WriteLine("请输入十个整数:");
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 奇偶数
{
class Program
{
static void Main(string[] args)
{
string[] str = new string[10];
for (int i = 0; i < str.Length; i++)
{
Console.Write("第" + i + "个数");
str[i]= Console.ReadLine();
}
for (int i = 0; i < str.Length; i++)
{
if (Convert.ToInt32(str[i]) % 2 == 0)
{
Console.WriteLine(str[i]);
}
}
Console.ReadLine();
}
}
}
第三种:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace 练习分拣奇数偶数
{
class Program
{
static void Main(string[] args)
{
string msg = "2 7 9 3 6 5 8 4";
string[] nums = msg.Split(' ');
//7 9 3 5 2 6 8 4
//存放奇数
ArrayList listOdd = new ArrayList();
//存放偶数
ArrayList listEven = new ArrayList();
for (int i = 0; i < nums.Length; i++)
{
if (Convert.ToInt32(nums[i]) % 2 != 0)
{
listOdd.Add(nums[i]);
}
else
{
listEven.Add(nums[i]);
}
}
listOdd.AddRange(listEven);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < listOdd.Count; i++)
{
sb.Append(listOdd[i]+" ");
}
Console.WriteLine(sb);
Console.ReadKey();
}
}
}