题目描述
命令行输入一个由数字组成的任意字符串,统计出每个数字出现的次数。
输入描述
1239586838
输出描述
0 counts:0
1 counts:1
2 counts:1
3 counts:2
4 counts:0
5 counts:1
6 counts:1
7 counts:0
8 counts:3
9 counts:1
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
String s = cin.nextLine();
int [] a = new int [12];
for (int i=0; i<s.length(); i++)
{
for (int j=48;j<=57;j++)
{
if ( s.charAt(i) == j )
{
a[j-48]++;
}
}
}
for (int i=0;i<=9;i++)
{
System.out.println(i+" counts:"+a[i]);
}
}
}