【发布时间】:2016-02-24 13:16:08
【问题描述】:
我目前正在尝试制作一个简单的程序来计算多个句子中元音的数量。输入如下所示:
6
艾米丽跳得很高
亚历克斯没有跳
菲利普讨厌跳跃
红龙会飞
三明治很好吃
我喜欢阳光
数字表示有多少行。我的问题是,当我打印出结果时,最后一行被忽略了。所以程序打印出 5 个整数,而不是 6 个。我已经玩了很长时间了,但我似乎无法理解这个问题。
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();
//System.out.println(a);
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;
}
【问题讨论】:
-
请注意,
i应该从 1 开始,否则在您的示例中您将获得 7 次迭代。 -
您能否将预期和实际输出也包括在内,因为我现在不明白您的问题是什么。
-
@KevinEsche 预期输出为 4 4 6 5 7 5,而实际输出为 4 4 6 5 7
-
我建议你在调试器中逐步检查你的代码,看看出了什么问题,阅读你的代码它应该为 0,1,2,3,4,5,6 迭代 7 次
-
您是否进行了任何更改,并且您确定您使用的是此版本的代码,因为它似乎产生了您期望的输出。