C#,用foreach语句编写循环遍历数组,找出个位数,十位数,百位数,千位数分别有多少个的程序

在线等,谢谢

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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numList = new int[] { 22, 444, 3, 5555, 6666 };
            //4位数或小于4位数
            int[] digit = new int[] { 0, 0, 0, 0 };
            foreach (int num in numList)
            {
                int n = num.ToString().Length;
                while (n > 0)
                {
                    digit[n - 1] += 1;
                    --n;
                }
            }
            Console.WriteLine("个位数{0},十位数{1},百位数{2},千位数{3}", digit[0],digit[1],digit[2],digit[3]);
            Console.ReadLine();
        }
    }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-06-08
int[] numList = new int[] { 1, 2, 3, 11, 22, 33, 111, 222, 333 };
string numList1 = "个位数:";
string numList2 = "十位数:";
string numList3 = "百位数:";
foreach (int i in numList)
{
if (i >= 0 && i < 10)
{
numList1 += i.ToString() + " ";
}
else
if (i >= 10 && i < 100)
{
numList2 += i.ToString() + " ";
}
else if (i >= 100 && i < 1000)
{
numList3 += i.ToString() + " ";
}
}
MessageBox.Show(numList1+numList2+numList3);本回答被网友采纳
第2个回答  2016-06-08
int.ToString().Length
根据长度来判断有多少位追问

可以写的详细点吗,本人纯小白,谢谢

追答

比如100
转为string之后是"100"
他的长度是3, 则存在千位数, 十位数和个位数
10=>"10"
长度2, 只有十位数和个位数

相似回答