6

该程序的目标是从包含所有 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;
}
4

4 回答 4

6

您可以在方法中简单地使用正则表达式:

public static boolean containsVowels(String word) {
    return Pattern.matches(".*a.*e.*i.*o.*u.*y.*", word);
}
于 2013-11-08T14:33:28.400 回答
3

使用正则表达式

if (word.matches("[^aeiou]*a[^aeiou]*e[^aeiou]*i[^aeiou]*o[^aeiou]*u[^aeiou]")){
    //found one
}

其中[^aeiou]*表示零个或多个辅音;^在正则表达式中表示[...中没有任何内容]

这可能不是最快的解决方案,但很清楚;[^aeiou]特别是如果您像我一样多次形成正则表达式而没有硬编码。

编辑:@Patrick 的正则表达式更胜一筹。

于 2013-11-08T14:34:08.810 回答
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();
}
于 2013-11-08T14:31:03.687 回答
2

没有正则表达式的逻辑。

public static boolean containsVowels(String word) throws NullPointerException {
    List<String> vowels = new ArrayList<String>(
                               Arrays.asList("a", "e", "i", "o", "u", "y"));
    String lastVowel = vowels.get(vowels.size() - 1);
    for (String c : vowels) {
        //false is returned if one vowel can't be found
        if (!word.contains(c)) {
            return false;
        }
        //true is returned once the last vowel is found (as the previous ones)
        if (c.equals(lastVowel)) {
            return true;
        }
    }
    //will never go out of the loop
}
于 2013-11-08T14:31:48.190 回答