---
title: "求一字符串中出现次数最多的字母和出现次数"
author: "befairy"
date: "2010-11-01T23:49:07Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-11-09T16:27:36Z"
id: 800143
url: "https://befairy.iteye.com/blog/800143"
---

```java

```


package zju;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class TheMostChar {

	/**
	 * 定义一串字母的字符串，求出现最多的那个字母，出现了多少次！ 
	 * @throws IOException 
	 */
	public static void main(String args) throws IOException {
		String str=null;
		int max=0;
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		System.out.print("请输入字符串：");
        str=br.readLine();
        br.close();
        Map<Character,Integer> m=new HashMap<Character,Integer>();
        for(int i=0;i<str.length();i++){
        	char c=str.charAt(i);
        	if(m.containsKey(c)){
        		m.put(c,m.get(c)+1);
        		if(max<m.get(c)+1){
        			max=m.get(c);
        		}
        	}else{
        		m.put(c,1);
        	}
        }
      
        Set<Character> S=m.keySet(); 
        for(char cc:S){
        	 
        	if(m.get(cc)==max){
        		System.out.println("出现次数最多的字符是"+cc+"一共出现"+max+"次");
        	}
        }
	}
    
}