统计字符的频率会出现异常?
public class frequency{
public static void main(String[] args){
String str="abcdefghijklmnsdjfwioeidsankjndkjfa9iewl";
int l=str.length();
int c[]=new int[l];
for(int k=0;k<l;k++){
int i=0;
while(i<l){
if(str.charAt(i)==str.charAt(i)&k!=i){
c[k]++;
i++;
}
}
for(int j=0;j<l;j++){
System.out.println(str.charAt(j)+" "+c[j]);
}
}
}
}
------解决方法--------------------------------------------------------
import java.util.*;
public class frequency{
public static void main(String[] args){
String str="abcdefghijklmnsdjfwioeidsankjndkjfa9iewl";
int l=str.length();
Hashtable ht=new Hashtable();
for(int k=0;k<l;k++){
if(ht.containsKey(new Character(str.charAt(k)))){
int value=((Integer)ht.get(new Character(str.charAt(k)))).intValue();
value++;
ht.put(new Character(str.charAt(k)),new Integer(value));
}
else{
ht.put(new Character(str.charAt(k)),new Integer(1));
}
}
System.out.println(ht);
}
}
------解决方法--------------------------------------------------------
楼主的while段明显是个死循环且if语句错
另外算法不好