java 随即数字 找出重复的数字(0-9)每个元素出现的次数

来源:百度知道 编辑:UC知道 时间:2024/05/15 05:49:04
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
随意输出 如(1657498321654687);

需要是 在此之下 加判断 列出如("0"出现几次)以此排开到("9"出现几次)

import java.util.*;
public class CalNums {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入一串数字: ");
String s = sc.nextLine();
int [] res = new int[10];
for(char c: s.toCharArray()){
res[(int)c-'0'] ++;
}
for(int i = 0; i < res.length; i ++) {
System.out.println(i + "出现了: " + res[i] + "次");
}

}
}

开一个数组
int[] count = new int[10];
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
for(char ch : s)
{
count[ch-'0']++;
}
得到count[0]是'0'的个数,count[1]是'1'的个数……