【发布时间】:2015-06-16 18:05:18
【问题描述】:
我正在尝试编写一个程序来声明和初始化一个字符数组(char[] word)并调用该方法:
public static int countVowels(char[])
返回单词中元音的数量。
谁能告诉我我哪里出错了?收到此错误
java:11: error: char cannot be dereferenced
for (int j=0; j < word[i].length(); j++) {
^
array.java:12: error: char cannot be dereferenced
char c = word[i].charAt(j);
^
2 errors
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++)
{
for (int j=0; j < word[i].length(); j++) {
char c = word[i].charAt(j);
if ( (c == 'a')
|| (c == 'e')
|| (c == 'i')
|| (c == 'o')
|| (c == 'u')
|| (c == 'A')
|| (c == 'E')
|| (c == 'I')
|| (c == 'O')
|| (c == 'U')
)
vowelCount++;
}
}
}
}
【问题讨论】: