for循环计算Java中的音节

for循环计算Java中的音节,java,loops,for-loop,count,Java,Loops,For Loop,Count,回到同一个项目的另一个问题! 该项目是第一学期的编程入门课程(Java),对于该项目,我们必须使用到目前为止所学的知识计算一些示例文本的FRI 我的问题是:如何使用for循环计算音节。到目前为止,我已经将它设置为遍历每个字母,检查元音,当它看到一个元音时停止,然后检查下一个字母是否有另一个元音或另一个字符 如果我用元音计算单词中的音节,并遵循前两条规则,这是可行的 然而,我不知道如何把一个没有元音的单词作为一个音节来计算 以下是我们必须遵循的音节计数规则: 单词中包含的音节数基于元音出现的次数,

回到同一个项目的另一个问题! 该项目是第一学期的编程入门课程(Java),对于该项目,我们必须使用到目前为止所学的知识计算一些示例文本的FRI

我的问题是:如何使用for循环计算音节。到目前为止,我已经将它设置为遍历每个字母,检查元音,当它看到一个元音时停止,然后检查下一个字母是否有另一个元音或另一个字符

如果我用元音计算单词中的音节,并遵循前两条规则,这是可行的

然而,我不知道如何把一个没有元音的单词作为一个音节来计算

以下是我们必须遵循的音节计数规则:

  • 单词中包含的音节数基于元音出现的次数,即字母a、e、i、o、u和 以下修改:
  • a。每组相邻的元音都算作一个音节。例如 “real、air、sound、tooth、soap”是一个单音节单词,即 “regal”有两个音节,“continuous”有三个音节,“queueing”只有两个音节 一个。“y”的作用不明确。它可以是一个辅音(“年”, “royal”,“yes”)和元音(“by”,“丑陋”,“许多”)。我们忽略了“y” 歧义,你的程序必须把它当作辅音

    b。单词末尾的“-e”或“-ed”不能算作音节 例如,单词“validate”有四个元音,但只有三个元音 音节。只有很少几个词的规则b消除了实际的错误 音节(例如,“t”或“d”之后的“-ed”不是无声的),例如 遗漏不会显著扭曲测试结果。你应该 还请注意,结尾有双“e”的单词不会丢失a 他们数的音节。例如,“委员会”有三个成员 音节数。该规则还可以增加计数,例如 根据规则,carefree有三个音节,但听起来像 有两个。有一个重要的例子,b被否决为 在下一节c中解释

    c。每个单词至少有一个音节,即使一个单词不包含音节 任何元音,或之前的规则给出的计数为零,它仍然是 被认为只有一个音节。例如,单词str,grrrr, pp,the,she,he,fed,led“都有一个音节

    以下是我目前掌握的代码:

    // 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==''
    可能永远不会发生

  • 忽略最终“a”、“i”、“u”、“o”:

    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