该计划的目标是从单词列表中返回包含所有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;
}
答案 0 :(得分:6)
您可以在方法中使用正则表达式:
public static boolean containsVowels(String word) {
return Pattern.matches(".*a.*e.*i.*o.*u.*y.*", word);
}
答案 1 :(得分:3)
使用正则表达式
if (word.matches("[^aeiou]*a[^aeiou]*e[^aeiou]*i[^aeiou]*o[^aeiou]*u[^aeiou]")){
//found one
}
[^aeiou]*
表示零个或多个辅音;正则表达式中的^
表示[
... ]
中没有任何内容。
它可能不是最快的解决方案,但很明显;特别是如果你形成正则表达式而没有像我一样多次硬编码[^aeiou]
。
编辑:@ Patrick的正则表达式更胜一筹。
答案 2 :(得分: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();
}
答案 3 :(得分: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
}