用c#编写程序:输入任意个数,求出这些数中奇数和偶数的个数

如题所述

对楼上的改进。。。

static void Main(string[] args)

{

    string str = Convert.ToString(Console.ReadLine());

    int j = 0, o = 0;

    foreach (string c in str.Split(','))//输入数据以逗号作为间隔,也可以用其他的

    {

       int i = Convert.ToInt32(c);

       if (i % 2 == 0)

       {

          o++;

       }

       else

       {

           j++;

       }

    }

Console.WriteLine("偶数是:{0}个", o);

Console.WriteLine("奇数是:{0}个", j);

Console.ReadKey();

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-10-11
看来你也没学多久吧,建个控制台应用程序
static void Main(string[] args)
{
string str = Convert.ToString(Console.ReadLine());
int j = 0, o = 0;
foreach (char c in str)
{
int i = Convert.ToInt32(c);
if (i % 2 == 0)
{
o++;
}
else
{
j++;
}

}
Console.WriteLine("偶数是:{0}个",o);
Console.WriteLine("奇数是:{0}个",j);
Console.ReadKey();
}
好好努力,加油
第2个回答  2009-10-13
简单得像 1 的题。对他们的改进,简化。
int[] sum = {20,21,23,24,26,30};//你的数
int sum1=0,sum2=0;//奇数和,偶数和

foreach(int temp in sum)
{
if(temp % 2 == 0)//偶数
sum2 += 1;
else
sum1 += 1;
}
相似回答