gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:50:50 27 4
gpt4 key购买 nike

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

最佳答案

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

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

关于java - 按字母顺序排列的元音单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19861286/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com