从键盘上输入10个整数,合法值为1,2或3,不是这3个数则为非法数字。试编程统计每个整数和非法数字的个数。
try
{
Console.WriteLine("请输入10个数字");
int[] nums = new int[10];
int[] count = new int[4];
for (int i = 0; i < nums.Length; i++)
{
Console.Write("请输入第{0}个数字", i + 1);
nums[i] = Convert.ToInt32(Console.ReadLine());
switch (nums[i])
{
case 1:
count[0] += 1;
break;
case 2:
count[1] += 1;
break;
case 3:
count[2] += 1;
break;
default:
count[3] += 1;
break;
}
}
Console.WriteLine("数字1的个数为{0}",count[0]);
Console.WriteLine("数字2的个数为{0}", count[1]);
Console.WriteLine("数字3的个数为{0}", count[2]);
Console.WriteLine("非法数字为{0}", count[3]);
}
catch
{
Console.WriteLine("你输入的有误,程序退出。");
}
Console.ReadKey();
网友评论