元音按字母顺序排列的单词

Tim*_*mmy 6 java

该程序的目标是从单词列表中返回包含所有6个元音(包括y)的单词.元音的字母顺序.例如,一个答案可能是这样的:Aerious(Aerious不会起作用,因为它没有ay).目前该计划不会返回任何文字.我不认为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;
}
Run Code Online (Sandbox Code Playgroud)

Pat*_*ick 6

您只需在方法中使用正则表达式:

public static boolean containsVowels(String word) {
    return Pattern.matches(".*a.*e.*i.*o.*u.*y.*", word);
}
Run Code Online (Sandbox Code Playgroud)


Plu*_*lux 2

仅当字符串“aeiouy”是单词的子字符串时, containsVowels 才会返回 true,如下所示:

"preaeiouy","aeiouypost","preaeiouypost"

这将是一个更正确的方法:

public static boolean containsVowels(String word) {
    String vowels = "aeiouy";
    if (word == null || word.length() < vowels.length())
        return false;
    int counter = 0;
    int vowelCounter = 0;
    //Loop until the whole word has been read, or all vowels found
    while(counter<word.length() && vowelCounter < vowels.length()){
        if (word.charAt(counter) == vowels.charAt(vowelCounter)){
            vowelCounter++;
        }
        counter++;
    }
    return vowelCounter == vowels.length();
}
Run Code Online (Sandbox Code Playgroud)