class wheel {
public static void main(String[] args) {
String[] phrase = {
"A STICH IN TIME SAVES NINE",
"DODNOT EAT YELLOW SNOW"
};
int[] letterCount = new int[26];
for (int count = 0; count < phrase.length; count++) {
String current = phrase[count];
char[] letters = current.toCharArray();
for (int count2 = 0; count2 < letters.length; count2++) {
char lett = letters[count2];
if ( (lett >= 'A') & (lett <= 'Z') ) {
letterCount[lett - 'A']++;
}
}
}
for (char count = 'A'; count <= 'Z'; count++) {
System.out.println(count + ":" +
letterCount[count - 'A'] +"\t");
}
System.out.println();
}
}
第一个for循环定义了一个名为count的整型变量,第三个for循环又定义了同名的字符变量,这样做可以吗? |