我正在尝试计算音节,现在我正在运行一些测试,以查看是否可以找到元音,然后再从那里开始。但是,我的输出结果为0,我看不到错误在哪里。

import java.util.*;
import java.io.*;

public class Word{

    private char[] letters;
    private char[] vowels;
    private int ct;
    private int temp;
    private int syllableCt;
    private int iftest;

    public Word(String[] words){
        temp = 0;
        ct = 0;
        for (int i = 0;i<words.length;i++){

            temp = countSyllables(words[i]);

        }

    }

    public int countSyllables(String str){

        char[] letters = str.toCharArray();
        syllableCt = 0;
        for (int i = 0; i<letters.length;i++){
            if (isVowel(letters[i]))
                syllableCt++;




            System.out.println("" + letters[i] + "\n");
            System.out.println("" + syllableCt + "\n");



        }
        return syllableCt;
    }
    public boolean isVowel(char ch){

        int iftest = 0;
        char[] vowels = { 'a', 'e', 'i', 'o', 'u', 'y','A','E','I','O','U','Y'};
        for (int i = 0;i<vowels.length;i++){
            if (ch == i)
                iftest = 1;

        }
        if (iftest == 1)
            return true;
        else
            return false;
    }

    public static void main(String args[]){
        String[] words;
        words = new String[5];
        words[0] = "dog";
        words[1] = "moon";
        words[2] = "syllables";
        words[3] = "reddit";
        words[4] = "3749832";
        Word word = new Word(words);
        System.exit(0);
    }
}

最佳答案

检查元音的方法似乎有误



if (ch == i)
          iftest = 1;


检查应为ch ==元音[i]。而且,方法和类可以大大地重构,在这里无济于事,因为我现在无法访问计算机。

查看以下方法是否有效:

public boolean isVowel(char ch){

  int iftest = 0;
  char[] vowels = { 'a', 'e', 'i', 'o', 'u', 'y','A','E','I','O','U','Y'};
  for (int i = 0;i<vowels.length;i++){
      if (ch == vowels[i])
          return true;

  }
 
      return false;
}

10-07 19:44