计算句子中包含正好两个元音的单词数 在一对“z”字符之间没有" z"它们之间。单词之间可能有多个空格,字母可以是大写或小写。显示每个句子中与模式匹配的单词数。
到目前为止,我有以下函数代码for循环,我需要知道如何完成它。请帮帮我。
以下是一些测试用例:
+-------------------------------------------+----------------+
| Sentence | Matching words |
+-------------------------------------------+----------------+
| a ZOO animal is a Zebraz | 1 |
| a ZOoZ animal is a Zebraz | 2 |
| the zebrazska goozed at the barking zEAlz | 2 |
| azoooza azoo ZORROrroozaster azurez | 1 |
| nothing much here | 0 |
+-------------------------------------------+----------------+
到目前为止,这是我的代码:
private boolean matches(String word)
{
int Vowels = 3;
for (int i=3; i<word.length(); ++i) {
if (word.charAt(i)=='z' )
{
return true;
} else {
}
}
return false;
}
答案 0 :(得分:0)
以下是我的意思:
static int countVowels(String atom)
{
int counter = 0;
for (int i = 0, l = atom.length(); i < l; i++) {
char c = atom.charAt(i);
if ("aeiouAEIOU".indexOf(c) != -1) {
counter++;
}
}
return counter;
}
static int countMatches(String word)
{
int count = 0;
int last_z = -1;
for (int i = 0, l = word.length(); i < l; i++) {
char c = word.charAt(i);
if ((c == 'z') || (c == 'Z')) {
if (last_z != -1) {
if (countVowels(word.substring(last_z, (i + 1))) == 2) {
count++;
}
}
last_z = i;
}
}
return count;
}
public static void main(String[] args)
{
int total = 0;
{
StringTokenizer st = (new StringTokenizer(args[0]));
while (st.hasMoreTokens()) {
total += countMatches(st.nextToken());
}
}
System.out.println(total);
}