最笨的统计字符串中数字的个数(控制台程序)

来源:互联网 发布:软件开发流程规范 编辑:程序博客网 时间:2024/05/17 23:05
 

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

namespace number
{
    class Program
    {
        static void Main(string[] args)
        {
            //等待用户输入字符串
            Console.WriteLine("请输入一串字符串");
            string str = Console.ReadLine();
            int j = str.Length;
            char[] a = new char[j];
            for (int i = 0; i < j; i++)
            {
                a[i] = Convert.ToChar(str.Substring(i, 1));
            }
            int[] count = new int[10];
            for (int k = 0; k < j; k++)
            {
                switch (a[k])
                {
                    case '0':
                        count[0]++;
                        break;
                    case '1':
                        count[1]++;
                        break;
                    case '2':
                        count[2]++;
                        break;
                    case '3':
                        count[3]++;
                        break;
                    case '4':
                        count[4]++;
                        break;
                    case '5':
                        count[5]++;
                        break;
                    case '6':
                        count[6]++;
                        break;
                    case '7':
                        count[7]++;
                        break;
                    case '8':
                        count[8]++;
                        break;
                    case '9':
                        count[9]++;
                        break;

                }
            }
            for (int i = 0; i < count.Length; i++)
            {
                Console.WriteLine("{0}的个数为{1}",i,count[i]);
            }
        }
    }
}

原创粉丝点击