【测开面试题-003】统计一个字符串中各个数字出现次数

package com.course.coke;

public class Que2 {

    public static void main(String[] args) {

        String s = "123004533";
        char[] array = s.toCharArray();
        int[] count = new int[10]; // 存放个数

        for (int i = 48; i < 58; i++) {
            int a = 0;
            for (int j = 0; j < array.length; j++) {
                if (array[j] == i){
                    a++;
                }
            }

            count[i-48] = a;
        }

        for (int i = 0; i < count.length; i++) {
            System.out.println(i + "的个数是:"+count[i]);
        }

    }
}

 

posted @ 2021-01-18 14:04  愚人李愚  阅读(94)  评论(0编辑  收藏  举报