public class StringAnalyzer
{
public static int numberOfVowels(String aWord)
{
int num = 0;
int length = aWord.length() -1;
int index = 0;
String vowels = "aeiouAEIOU";
while(index <= length)
{
aWord = aWord.substring(index, index +1);
num++;
}
return num;
}
public static void main(String[] args)
{
numberOfVowels("derp");
}
}
答案 0 :(得分:-1)
public static int numberOfVowels(String aWord) {
int num = 0;
final String vowels = "aeiouAEIOU";
for (int i = 0; i < aWord.length(); i++) {
if (vowels.indexOf(aWord.substring(i, i+1)) != -1) {
num++;
}
}
return num;
}
符合所有要求,仅使用substring和indexOf。
substring(i,i + 1)获取索引i处的char作为java.lang.String,indexOf检查元音字符串中是否存在包含一个字符的字符串。