Java 数组中的元音数

Java 数组中的元音数,java,arrays,Java,Arrays,我正在尝试编写一个程序,它声明并初始化一个字符数组(char[]word)并调用该方法: public static int countVowels(char[]) 返回单词中元音的数量 谁能告诉我哪里出了问题?获取此错误 java:11:错误:无法取消对字符的引用 对于(intj=0;j

我正在尝试编写一个程序,它声明并初始化一个字符数组(char[]word)并调用该方法:

public static int countVowels(char[])
返回单词中元音的数量

谁能告诉我哪里出了问题?获取此错误

java:11:错误:无法取消对字符的引用
对于(intj=0;j

公共类数组{
公共静态void main(字符串[]args){
char[]word={'a','b','f','u','g','i','o','r'};
}
公共静态整数元音(字符[]单词){
int-vouelcount=0;
for(int i=0;i
您实际上不需要内部循环。请尝试以下方法:

public class array { 
 public static void main(String[] args) { 
  char[] word = {'a','b','f','u','g','i','o','r'};

}
public static int countVowels(char[] word) {
int vowelCount = 0; 

for (int i = 0; i < word.length; i++) 
{ 
char c = word[i]; 
if ( (c == 'a') 
|| (c == 'e') 
|| (c == 'i') 
|| (c == 'o') 
|| (c == 'u') 
|| (c == 'A') 
|| (c == 'E') 
|| (c == 'I') 
|| (c == 'O') 
|| (c == 'U') 
) 
vowelCount++; 
  }
return vowelCount;
  } 
}
公共类数组{
公共静态void main(字符串[]args){
char[]word={'a','b','f','u','g','i','o','r'};
}
公共静态整数元音(字符[]单词){
int-vouelcount=0;
for(int i=0;i
尝试
word.length
。该代码不应在浏览器中运行,因为它不是javascript。而是java。

调用word[i]时,您将得到存储在数组word第i个位置的值。因此,word[i].length返回存储在第i个位置的值的长度。由于存储的值是没有长度属性的字符,因此出现错误。请尝试使用word.length。这将为您提供数组的长度


有了这些信息,您应该有足够的时间修复for循环代码。请记住,word[i]返回一个字符。

您有多个错误

就用这些,

for (int j=0; j < word.length; j++) { 
char c = word[i];
for(int j=0;j
而不是下面的这些(这些是无效的)

for(int j=0;j
要获得数组的长度,长度就足够了,您不必放置副词。而且,当您指定单词数组的ith char元素时,c=word[i];是有效的

我刚刚纠正了您的方法,并为其编写了测试代码。您还使用了不必要的外部for循环,这导致了无效的结果

除了您更正的方法之外,我还添加了计算元音的方法,该方法使用二级数组存储元音。它可读性更强,但选择权在您;)

公共类TestCount元音{
公共静态void main(字符串[]args){
char[]word={'a','b','f','u','g','i','o','r'};
//我建议调用gevouelcount(char[])方法
System.out.println(“元音计数:+getvouelcount(word));
//用我的更正调用您的方法
System.out.println(“元音计数:+yourCountMethod(word));
}
//比较字符数组和计数的方法
公共静态整型GetVouelCount(字符[]输入字){
char[]元音={'a','a','e','e','i','i','o','o','u','u'};
int-vouelcount=0;
for(int i=0;i
希望它能有所帮助

for (int j=0; j < word.length; j++) { 
char c = word[i];
for (int j=0; j < word[i].length(); j++) { 
char c = word[i].charAt(j);
public class TestCountVowels {

    public static void main(String[] args) {
        char[] word = {'a','b','f','u','g','i','o','r'};

        //I advice calling geVowelCount(char[]) method
        System.out.println("Vowel count: " + getVowelCount(word) );

        //Calling your method with my corrections
        System.out.println("Vowel count: " + yourCountMethod(word) );


    }

    //My method for comparing char arrays and counting
    public static int getVowelCount(char[] inputWord) {
        char[] vowels = {'a','A','e','E','i','I','o','O','u','U'};
        int vowelCount = 0;

        for( int i = 0; i < inputWord.length; i++ ) {
            for( int j = 0; j < vowels.length; j++ )
                if ( inputWord[i] == vowels[j] ) {
                    vowelCount++;
                    continue;
                }
        }

        return vowelCount;
    }

    //Your method with corrections
    public static int yourCountMethod(char[] word) {
        int vowelCount = 0; 

        for (int i = 0; i < word.length; i++) 
        {
            char c = word[i];
            if (
                    (c == 'a') || 
                    (c == 'e') || 
                    (c == 'i') || 
                    (c == 'o') || 
                    (c == 'u') || 
                    (c == 'A') || 
                    (c == 'E') || 
                    (c == 'I') || 
                    (c == 'O') || 
                    (c == 'U') 
                ) 
                vowelCount++; 
        }

        return vowelCount;
    }

}