0
public class AssignmentChapter9
{
public static void main(String[] args)
{
String words = Input.getString("Please enter a series of words with the spaces omitted.");
String alphabet = "abcdefghijklmnopqrstuvwxyz";
String inputWords = words.toLowerCase();
int lcount[] = new int[26];
char var1;
char var2;
for(int x = 0; x <= words.length(); x++)
{
var1 = words.charAt(x);
for(int y = 0; y < 27; y++)
{
var2 = alphabet.charAt(y);
if(var1 == var2)
{
lcount[y] += 1;
}
}
}
for(int z = 0; z < 27; z++)
{
System.out.println("Letter " + alphabet.charAt(z + 1) + " count = " + lcount[z]);
}
}
}
我一直在嘗試在java中編寫程序來確定字母表中每個字符出現在給定字符串中的次數。我能夠成功編譯程序,但在用戶輸入完成後,它會出現超出界限的異常。任何幫助,將不勝感激。使用字符串數組的字母計數器