0

我目前正在尝试制作一个简单的程序来计算多个句子中元音的数量。输入如下所示:

6

艾米丽跳得很高

亚历克斯没有跳

菲利普讨厌跳跃

红龙会飞

三明治很不错

我喜欢阳光

数字表示有多少行。我的问题是,当我打印出结果时,最后一行被忽略了。所以程序打印出 5 个整数,而不是 6 个。由于某种原因,当我发布输入时,它会自动显示前 5 个整数,然后我只需要按 Enter 键就可以显示最后一个整数。

Scanner in = new Scanner(System.in);
int cases = in.nextInt(); //how many lines there are 
String a; 
int vowels = 0;
int length; //length of a line
char temp;
in.nextLine(); //skips first line of the input, as this declares how many lines there are

for (int i = 0; i < cases; i++) 
{
    a = in.nextLine();
    length = a.length();
    
    for (int b = 0; b < length; b++)
    {
        temp = a.charAt(b);
        if (temp == 'a' || temp == 'e' || temp == 'i' || temp == 'o' || temp == 'u')
        {
            vowels++;
        }
    }
    System.out.println(vowels);
    vowels=0;
}
4

0 回答 0