刚学JAVA一年,写了个练练手,里面很多变量名不规范,而且没做相同数量的比较。
package test;
import java.util.HashMap;import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Test {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
String sssString= scanner.next();
SearchMaxStr(sssString);
}
}
public static void SearchMaxStr(String str){
Map <String,Integer> map = new HashMap<String,Integer>();
if(null!=str&&""!=str){
char [] arr = str.toCharArray();
for(int i = 0;i<arr.length;i++){
int temp = 1;
if(' '==arr[i]){
continue;
}else{
if(map.containsKey(arr[i]+"")){
int sum = map.get(arr[i]+"");
sum++;
map.put(arr[i]+"", sum);
}else{
map.put(arr[i]+"", temp);
}
}
}
StringBuffer buffer = new StringBuffer();
Set<String> set = map.keySet();
int count = 0;
for (Object object : set) {
if(map.get(object)>count){
count = map.get(object);
buffer.append(object);
}
}
char cc = buffer.charAt(buffer.length()-1);
System.out.println("出现最多的字母 :"+cc+"--->出现的次数 :"+map.get(cc+""));
}
}
}