public class CharArrayProgram {
public static void main(String[] args) {
String[] wordsArray = {"hello", "world", "apple", "orange", "elephant"};
int count = 0;
for (String word : wordsArray) {
if (containsTwoVowelsInARow(word)) {
System.out.println("Found: " + word);
count++;
}
}
System.out.println("Total number of words with two vowels in a row: " + count);
}
private static boolean containsTwoVowelsInARow(String word) {
for (int i = 0; i < word.length() - 1; i++) {
if (isVowel(word.charAt(i)) && isVowel(word.charAt(i + 1))) {
return true;
}
}
return false;
}
private static boolean isVowel(char c) {
return "aeiouAEIOU".indexOf(c) != -1;
}
}
在这个程序中,我们遍历给定的字符串数组,对于每个单词,我们检查它是否包含两个毗邻位置有元音字母,如果有,我们将计数器加一,并输出该单词。最后,我们打印出有多少单词包含两个毗邻位置有元音字母。我们使用私有静态方法isVowel(char c)来检查一个字符是否为元音字母,并使用私有静态方法containsTwoVowelsInARow(String word)来检查一个字符串是否包含两个毗邻位置有元音字母。