我的作业要求我阅读一个文件并打印每行中元音的数量。出于某种奇怪的原因,它在某些行中打印了适量的元音,但在其他行中却没有。为什么要这样做?!
这是我计算元音的方法:
public static int calculateVowels(String line)
{
char[] vowels = new char[] {'a','e','i','o','u'};
// count the number of vowels in a line
int vowelCount = 0;
for (int i = 0; i < line.length(); i++) {
char a = line.charAt(i);
for (char vowel : vowels) {
if (a == vowel){
vowelCount++;
break;
}
}
}
return vowelCount;
}
这是我在 main 中调用它的时候:
while ((line = br2.readLine()) != null)
{
lineCount++;
// count the number of words in a line
String[] words = line.split(" ");
if (words != null)
wordCount += words.length;
int vowelCount = calculateVowels(line);
System.out.println("Line " + lineCount + " has " + vowelCount + " vowels.");
}
有任何想法吗?!谢谢!