【问题标题】:Number of vowels within an array数组中的元音数
【发布时间】: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++; 
   } 
  } 
 } 
}

【问题讨论】:

    标签: java arrays


    【解决方案1】:

    您实际上并不需要内部循环。试试这个:

    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;
      } 
    }
    

    【讨论】:

      【解决方案2】:

      当您调用 word[i] 时,您将获得存储在数组 word 中第 i 个位置的值。因此, word[i].length 返回存储在第 i 个位置的值的长度。你得到一个错误,因为存储的值是一个字符,它没有长度属性。相反,只尝试 word.length。这将为您提供数组的长度。

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

      【讨论】:

        【解决方案3】:

        试试word.length。该代码不应在浏览器中运行,因为它不是 javascript。相反,它是 java。

        【讨论】:

          【解决方案4】:

          你有不止一个错误;

          就用这些吧;

          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);
          

          要得到数组的长度,长度就够了,不用放括号。而且,当您分配单词数组的第 i 个字符元素时,c = word[i]; 是有效的。

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

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

          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;
              }
          
          }
          

          希望对你有帮助。

          【讨论】:

            猜你喜欢
            • 2018-07-26
            • 2021-01-31
            • 2021-08-07
            • 1970-01-01
            • 1970-01-01
            • 2022-01-21
            • 2021-05-27
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多