for循环计算Java中的音节
回到同一个项目的另一个问题! 该项目是第一学期的编程入门课程(Java),对于该项目,我们必须使用到目前为止所学的知识计算一些示例文本的FRI 我的问题是:如何使用for循环计算音节。到目前为止,我已经将它设置为遍历每个字母,检查元音,当它看到一个元音时停止,然后检查下一个字母是否有另一个元音或另一个字符 如果我用元音计算单词中的音节,并遵循前两条规则,这是可行的 然而,我不知道如何把一个没有元音的单词作为一个音节来计算 以下是我们必须遵循的音节计数规则:for循环计算Java中的音节,java,loops,for-loop,count,Java,Loops,For Loop,Count,回到同一个项目的另一个问题! 该项目是第一学期的编程入门课程(Java),对于该项目,我们必须使用到目前为止所学的知识计算一些示例文本的FRI 我的问题是:如何使用for循环计算音节。到目前为止,我已经将它设置为遍历每个字母,检查元音,当它看到一个元音时停止,然后检查下一个字母是否有另一个元音或另一个字符 如果我用元音计算单词中的音节,并遵循前两条规则,这是可行的 然而,我不知道如何把一个没有元音的单词作为一个音节来计算 以下是我们必须遵循的音节计数规则: 单词中包含的音节数基于元音出现的次数,
// While loop to count words and increment syllable count per word
while(countWords.hasNext()){
String word = countWords.useDelimiter(WORD_DELIMITERS).next();
if(word.compareTo("") != 0){
numWords++;
for(k=0; k < word.length(); k++){
char letter = word.charAt(k);
if(letter == 'A' || letter == 'a'
|| letter == 'I' || letter == 'i'
|| letter == 'O' || letter == 'o'
|| letter == 'U' || letter == 'u'){
if(k+1 < word.length()){
char nextLetter = word.charAt(k+1);
if(nextLetter == 'A' || nextLetter == 'a'
|| nextLetter == 'E' || nextLetter == 'e'
|| nextLetter == 'I' || nextLetter == 'i'
|| nextLetter == 'O' || nextLetter == 'o'
|| nextLetter == 'U' || nextLetter =='u'
|| nextLetter == ' '){
numSyllables++;
}
else{
numSyllables++;
}
}
}
else if(letter == 'E' || letter == 'e'){
if(k+1 < word.length()){
char nextLetter = word.charAt(k+1);
if(nextLetter != 'D' || nextLetter != 'd'
|| nextLetter != 'A' || nextLetter != 'a'
|| nextLetter != 'I' || nextLetter != 'i'
|| nextLetter != 'O' || nextLetter != 'o'
|| nextLetter != 'U' || nextLetter !='u'
|| nextLetter == ' '){
numSyllables++;
}
}
}
}
if(numSyllables == 0){
numSyllables = 1;
}
System.out.println((numWords) + " " + word + " " + numSyllables);
numSyllables = 0;
}
}
//While循环计数单词并增加每个单词的音节计数
while(countWords.hasNext()){
String word=countWords.useDelimiter(word_DELIMITERS).next();
if(word.compareTo(“”)=0){
numWords++;
for(k=0;k
打印输出示例:
1你好12世界13这是15只是16 a 17测试18
1 9参见1 11我的1 12计划2 13如何工作1 14向上1 15
到116这117点218我很清楚我
1 22算出了2 25个音节1 26个东西1 27但是
1 28我1 29不1 30确定1
评论你的代码
以下是对您发布的代码的一些评论:
word.compareTo("") != 0
这有点奇怪<代码>比较通常按字母顺序进行。也许你想用!word.equals(“”)
if…else…
if(nextLetter == 'A' || nextLetter == 'a'
|| nextLetter == 'E' || nextLetter == 'e'
|| nextLetter == 'I' || nextLetter == 'i'
|| nextLetter == 'O' || nextLetter == 'o'
|| nextLetter == 'U' || nextLetter =='u'
|| nextLetter == ' '){
numSyllables++;
}
else{
numSyllables++;
}
因此,如果条件是true
或false
,那么您将得到numSyllables++
,因此没有必要让条件。。。此外,如果您只有无空格的单词,nextLetter==''
可能永远不会发生if (letter == 'A' ...){
if(k+1 < word.length()){
...
}
}
如果nextLetter
是D或D
:它是D
,那么nextLetter!='d'
为true,否则,nextLetter!='D'
应该已经是真的
,所以下一个测试永远不会发生。另外,这里不要忽略最后的“ed”,因为如果它是ed
,nextLetter!='d'
为真,如果为ed
则nextLetter!='D'
为真if(nextLetter != 'D' || nextLetter != 'd'
|| nextLetter != 'A' || nextLetter != 'a'
|| nextLetter != 'I' || nextLetter != 'i'
|| nextLetter != 'O' || nextLetter != 'o'
|| nextLetter != 'U' || nextLetter !='u'
|| nextLetter == ' '){
numSyllables++;
}
private static boolean isVowel(char letter) {
return letter == 'A' || letter == 'a'
|| letter == 'E' || letter == 'e'
|| letter == 'O' || letter == 'o'
|| letter == 'I' || letter == 'i'
|| letter == 'U' || letter == 'u';
}
private static int countWithLoop(String word) {
// start counting
int syllableCount = 0;
// use a while loop
int index = 0;
while (index < word.length()) {
char letter = word.charAt(index);
// if vowel:
if (isVowel(letter)) {
// specific case of "E"/"e"
if (letter == 'E' || letter == 'e') {
// 1. last "e" is ignored:
if (index == word.length() - 1) {
index++;
}
// 2. if "ed" finished the word, it is ignored
else if (index == word.length() - 2
&& (word.charAt(word.length() - 1) == 'd' || word.charAt(word.length() - 1) == 'D')) {
index++;
}
// 3. this is neither a last "e" or last "ed". Count as a syllable
else {
// count one more syllable...
syllableCount++;
// ...and skip consecutive vowel
while (isVowel(letter) && index < word.length() - 1) {
index++;
letter = word.charAt(index);
}
}
} else {
// count one more syllable...
syllableCount++;
// ...and skip consecutive vowel
while (isVowel(letter) && index < word.length() - 1) {
index++;
letter = word.charAt(index);
}
}
}
// otherwise, keep going
else {
index++;
}
}
// return
return Math.max(1, syllableCount);
}
private static int countWithRegex(String word) {
String i = "(?i)[aiou][aeiou]*|e[aeiou]*(?!d?\\b)";
Matcher m = Pattern.compile(i).matcher(word);
int count = 0;
while (m.find()) {
count++;
}
// return at least 1
return Math.max(count, 1);
}
(?i)[aiou][aeiou]*|e[aeiou]*(?!d?\\b) = complete regex
(?i) = case insensitivity: A=a, B=b and so on
[aiou] = look for letter "a", "i", "o", "u", and their
capital letter counterpart
* = look for the characters between the bracket
with zero, one or more occurences
[aiou][aeiou]* = look for non-E vowel followed a zero, one
or more vowel. Like "a", or "Oa", or "ueu"
| = Or ...
e = simple look for letter "e" or "E"
[aeiou]* = same as above: look for "e" followed by
consecutive vowels
(?!.....) = but exclude "e"/"E" when followed by...
d?\\b = ...an optional "d"/"D" letter and \\b refer
to the end of the String: it excludes final
"e" and final "ed"
public static void main(String... aArgs) {
List<String> words = new ArrayList<>();
words.add("real");
words.add("air");
words.add("sound");
words.add("tooth");
words.add("soap");
words.add("regal");
words.add("continuous");
words.add("queueing");
words.add("committee");
words.add("carefree");
words.add("grrrr");
words.add("she");
words.add("fed");
// Challenge the "ed" but not at the end of the word
words.add("Medecine");
// Challenge a final "e"
words.add("Stone");
// Challenge a final "ed"
words.add("Stoned");
// Challenge a double vowel
words.add("Year");
// Challenge case sensitivity
words.add("BoAr");
// Expected answer depends on the number of drink Andy has taken
words.add("Vacuum");
System.out.println("----Counting with Loop----");
for (int i = 0; i < words.size(); i++) {
System.out.println(i + " " + words.get(i) + " " + countWithLoop(words.get(i)));
}
System.out.println("----Counting with Regex----");
for (int i = 0; i < words.size(); i++) {
System.out.println(i + " " + words.get(i) + " " + countWithRegex(words.get(i)));
}
}
----Counting with Loop----
0 real 1
1 air 1
2 sound 1
3 tooth 1
4 soap 1
5 regal 2
6 continuous 3
7 queueing 1
8 committee 3
9 carefree 3
10 grrrr 1
11 she 1
12 fed 1
13 Medecine 3
14 Stone 1
15 Stoned 1
16 Year 1
17 BoAr 1
18 Vacuum 2
----Counting with Regex----
0 real 1
1 air 1
2 sound 1
3 tooth 1
4 soap 1
5 regal 2
6 continuous 3
7 queueing 1
8 committee 3
9 carefree 3
10 grrrr 1
11 she 1
12 fed 1
13 Medecine 3
14 Stone 1
15 Stoned 1
16 Year 1
17 BoAr 1
18 Vacuum 2