问题描述
音节的规范:
每组相邻的元音(a,e,i,o,u,y)都算作一个音节(例如,"real"中的"ea"贡献一个音节,但是"e ... a"在富豪"中算作两个音节).但是,单词末尾的"e"不算作音节.而且,即使先前的规则计数为零,每个单词也至少有一个音节.
Each group of adjacent vowels (a, e, i, o, u, y) counts as one syllable (for example, the "ea" in "real" contributes one syllable, but the "e...a" in "regal" counts as two syllables). However, an "e" at the end of a word doesn't count as a syllable. Also each word has at least one syllable, even if the previous rules give a count of zero.
我的countSyllables方法:
public int countSyllables(String word) {
int count = 0;
word = word.toLowerCase();
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == '\"' || word.charAt(i) == '\'' || word.charAt(i) == '-' || word.charAt(i) == ',' || word.charAt(i) == ')' || word.charAt(i) == '(') {
word = word.substring(0,i)+word.substring(i+1, word.length());
}
}
boolean isPrevVowel = false;
for (int j = 0; j < word.length(); j++) {
if (word.contains("a") || word.contains("e") || word.contains("i") || word.contains("o") || word.contains("u")) {
if (isVowel(word.charAt(j)) && !((word.charAt(j) == 'e') && (j == word.length()-1))) {
if (isPrevVowel == false) {
count++;
isPrevVowel = true;
}
} else {
isPrevVowel = false;
}
} else {
count++;
break;
}
}
return count;
}
isVowel方法,用于确定字母是否为元音:
public boolean isVowel(char c) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
return true;
} else {
return false;
}
}
根据一位同事,在 此文本 ,但我似乎可以使它等于那个,而且我不知道我们哪个是正确的.请帮助我将我的方法发展为正确的算法,或帮助表明这是正确的.谢谢.
According to a colleague, this should result in 528 syllables when used on this text, but I can seem to get it to equal that and I don't know which of us is correct. Please help me develop my method into the correct algorithm or help show this is correct. Thank you.
推荐答案
问题之一可能是您在输入中调用了恋人案例方法,但未分配它.
One of the problem might be that you call to lover case method on the input, but you do not assign it.
因此,如果您更改
word.toLowerCase();
到
word = word.toLowerCase();
肯定会有所帮助.
这篇关于Java-根据规范编写音节计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!