天天看點

輸入一個字元串,擷取每個字元出現的次數

Scanner sc = new Scanner(System.in);

String str = sc.nextLine();
	
	while(str.length() != 0){
		//擷取原字元串的長度
		int oldLength = str.length();
		
		//擷取字元串的第一個字元
		char c = str.charAt(0);
		
		//替換str中所有和C相同的字元
		str = str.replaceAll(c+"", "");
		
		System.out.println(c+"出現的次數是:"+(oldLength - str.length()));
		
	}
	

}