C#编程实践51题目解答

编程实现,输入n个儿童的年龄,统计这n个儿童中共有多少个不同的年龄。

 int[] intA;
            string[] strA;
            int intB, i;
            intA = new int[13];
            strA = new string[13];
            i = 1;

            do
            {
                Console.Write("请输入儿童的年龄(输入-1结束统计输出结果):\n");
                intB = Convert.ToInt32(Console.ReadLine());

                /*********跳出统计判断*/
                if (intB == -1)
                {
                    break;
                }
                /*********年龄是否符合要求判断*/
                if (intB > 12 || intB <= 0)
                {
                    Console.Write("\n您输入的不是儿童年龄\n儿童年龄为1~12");
                    continue;
                }
                else
                {
                    /*********符合要求后统计*/
                    strA[intB] = strA[intB] + i.ToString() + "";
                    intA[intB]++;
                    i++;
                }


            } while (true);
            /*********输出结果*/
            for (int j = 1; j < 13; j++)
            {
                /*********输出优化*/
                if (intA[j] != 0)
                {
                    Console.Write("{0}岁的儿童有:{1}个,为第" + strA[j] + "输入的儿童\n", j, intA[j]);
                }
            }

            Console.ReadKey();
原文地址:https://www.cnblogs.com/Wzqa/p/2769621.html