您的位置:首页 > 其它

统计一个字符串中每个字符出现的次数

2016-11-01 14:37 459 查看
public class Test{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String result = "";
result = sc.nextLine();
System.out.println("输入:"+result);
while(!"".equals(result)){
char a = result.charAt(0);
String first = result.substring(0,1);
System.out.print(first+"("+getCount(result,a)+") ");
result = result.replaceAll(first,"");
}
}

public static int getCount(String str,char a){
int count = 0;
for(int i = 0;i<str.length();i++){
if(str.charAt(i) == a){
count++;
}
}
return count;
}
}
例:
输入:aabdndj23334NJSH你好你好
a(2) b(1) d(2) n(1) j(1) 2(1) 3(3) 4(1) N(1) J(1) S(1) H(1) 你(2) 好(2) 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐