C#中,编写控制台应用程序项目,读入一组整数(以输入0结束)分别求出奇偶数之和

2024-11-26 14:32:33
推荐回答(1个)
回答1:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

string strtemp = Console.ReadLine();

Console.WriteLine("奇数和:{0}"答亏,jisuanjishuhe(strtemp));
Console.WriteLine("偶数和:{0}", jisuanoushuhe(strtemp));
Console.WriteLine("按回车退出");
Console.ReadLine();
}

static int jisuanjishuhe(string str)
{
int temp=0;
foreach (char c in str)
{
int i=(Convert.ToInt32(c)-48);
if ( i% 2 != 0)
{
temp += i;
}
}

return temp;
}

static int jisuanoushuhe(string str)
{
int temp = 0;
foreach (char c in str)
{
int i = (Convert.ToInt32(c) - 48);
if (i % 2 == 0)
{
temp += i;
}
}

return temp;
}
}
}

------------------
写拆运代清御神码辛苦,采纳吧