java语言程序设计第八版9.5**

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner input = new Scanner(System.in);
	System.out.print("Enter a string: ");
	String string = input.nextLine();

	count(string);
}

public static int[] count(String s) {
	int[] array = new int[10];
	for (int j = 0; j < array.length; j++) {
		array[j] = 0;
	}
	
	for (int i = 0; i < s.length(); i++) {
		if(Character.isDigit(s.charAt(i))){
			array[s.charAt(i) - 48] += 1; 
		}
	}
	
	for (int m = 0; m < array.length; m++) {
		if(array[m] != 0)
			System.out.println("counts[" + m + "] is " + array[m]); 
	}
	return array;
	 
} 

你可能感兴趣的:(java语言程序设计第八版9.5**)