C# 运用数组 如何输入十个数 找出区中的奇数偶数 并计算他们的个数
小弟初学者 对编程有着浓厚的兴趣,遇到个问题( C# 运用数组 如何输入十个数 找出区中的奇数偶数 并分贝计算奇数和偶数的个数)希望大家传授些经验如何比较有效率的学C#
程序代码:using System;
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());
}
}
}
}
}