程序代码:
import java.util.Scanner;
public class Kn {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter a string: ");
String s=input.nextLine();
int[] counts=countLetters(s.toLowerCase());
for(int i=0;i
if(counts[i]!=0)
System.out.println((char)('a'+i)+" appears "+counts[i]+((counts[i]==1)?"time":"times"));
}
}
public static int[] countLetters(String s){
int[] counts=new int[26];
for(int i=0;i
if(Character.isLetter(s.charAt(i)))
counts[s.charAt(i)-'a']++;
}
return counts;
}
}
运行结果:
Enter a string: addaddawdd
a appears 3times
d appears 6times
w appears 1time