该程序的目标是从包含所有 6 个元音(包括y
)的单词列表中返回单词。元音按字母顺序排列的地方。例如,答案可能类似于:Aerious(尽管 Aerious 不起作用,因为它没有y
)。目前该程序不返回任何单词。我不认为containsVowels
方法是正确的。
public static void question11() {
System.out.println("Question 11:");
System.out.println("All words that have 6 vowels once in alphabetical order: ");
String vowelWord = "";
for (int i = 1; i< WordList.numWords(); i++) {
if (containsVowels(WordList.word(i))) {
if (alphabetical(WordList.word(i))) {
vowelWord = WordList.word(i);
System.out.println(vowelWord);
}
}
}
return;
}
public static boolean alphabetical(String word) {
int vowelPlaceA = 0;
int vowelPlaceE = 0;
int vowelPlaceI = 0;
int vowelPlaceO = 0;
int vowelPlaceU = 0;
int vowelPlaceY = 0;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == 'a') {
vowelPlaceA = i;
}
if (word.charAt(i) == 'e') {
vowelPlaceE = i;
}
if (word.charAt(i) == 'i') {
vowelPlaceI = i;
}
if (word.charAt(i) == 'o') {
vowelPlaceO = i;
}
if (word.charAt(i) == 'u') {
vowelPlaceU = i;
}
if (word.charAt(i) == 'y') {
vowelPlaceY = i;
}
//check a alphabetical
if(vowelPlaceA > vowelPlaceE || vowelPlaceA > vowelPlaceI || vowelPlaceA > vowelPlaceO ||
vowelPlaceA > vowelPlaceU || vowelPlaceA > vowelPlaceY) {
return false;
}
//check e alphabetical
if(vowelPlaceE > vowelPlaceI || vowelPlaceE > vowelPlaceO ||
vowelPlaceE > vowelPlaceU || vowelPlaceE > vowelPlaceY) {
return false;
}
//i
if(vowelPlaceI > vowelPlaceO || vowelPlaceI > vowelPlaceU || vowelPlaceI > vowelPlaceY) {
return false;
}
//o
if(vowelPlaceO > vowelPlaceU || vowelPlaceO > vowelPlaceY) {
return false;
}
//u
if(vowelPlaceU > vowelPlaceY) {
return false;
}
}
return true;
}
public static boolean containsVowels(String word) {
String vowels = "aeiouy";
if (word.contains(vowels)) {
return true;
}
return false;
}