JAVA代码,输入一组数字,个数未知,判断中间相同数字出现的次数!例如1 2 5 4 8 2 7

JAVA代码,输入一组数字,个数未知,判断中间相同数字出现的次数!例如1 2 5 4 8 2 7 3 4

1出现一次
2出现两次
3出现一次
……

import java.util.Scanner;
import java.util.TreeMap;
/**
 * 从键盘输入16位长整数,编程统计每个数字出现的个数
 * @author young
 *
 */
public class CharMapDemo {
public static TreeMap<Character, Integer> Pross(String str) {
char[] charArray = str.toCharArray();

TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();

for (int x = 0; x < charArray.length; x++) {
if (!tm.containsKey(charArray[x])) {
tm.put(charArray[x], 1);
} else {
int count = tm.get(charArray[x]) + 1;
tm.put(charArray[x], count);
}
}
return tm;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个长整数:");
int temp = sc.nextInt();
String str = String.valueOf(temp);
TreeMap<Character, Integer> tm = Pross(str);
System.out.println(tm);
}
}

追问

还没有学map,能不能用循环和方法做啊,谢谢

温馨提示:答案为网友推荐,仅供参考