C#中,编写控制台应用程序项目,读入一组整数(以输入0结束)分别求出奇偶数之和
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;
}
}
}
------------------
写代码辛苦,采纳吧
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;
}
}
}
------------------
写代码辛苦,采纳吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询