static void Main(string[] args)
{
int a=0;
int x = 0, y = 0;
string str = "";
while (true)
{
while (true)
{
try
{
Console.WriteLine("请输入一个整数:");
str = Console.ReadLine();
if (str == null)
{
continue;
}
a = Convert.ToInt32(Regex.Replace(str, "[^\\d]", ""));
if (Convert.ToString(a).Length != 0 && a != 0)
{
break;
}
else
{
Console.WriteLine("输入有误,重新输入:");
}
}
catch (Exception e)
{
Console.WriteLine("输入有误,重新输入:");
continue;
}
}
int i = a;
while (a != 0)
{
if (a % 10 % 2 != 0)
{
x++;
}
else
{
y++;
}
a /= 10;
}
Console.WriteLine("数字:{0};\t奇数有:{1}个;\t偶数有:{2}个", i, x, y);
x =y = 0;
Console.WriteLine("----------输入1继续,否者退出-----------");
str= Console.ReadLine();
if (!str.Contains("1"))
{
break;
}
}
Console.ReadKey();
}
建议你这样试试看:
先将数字用ToString()转化成字符串,再逐字符判断是否属于“13579”和"02468"这两个字符串。