数组中的元音数量

时间:2015-06-16 18:05:19

标签: java arrays

我正在尝试编写一个程序来声明和初始化一个字符数组(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++; 
   } 
  } 
 } 
}

4 个答案:

答案 0 :(得分:2)

你真的不需要内循环。试试这个:

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

答案 1 :(得分:1)

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

有了这些信息,你应该有足够的东西来修复你的for循环代码。记住,word [i]返回一个char。

答案 2 :(得分:0)

试试 /// <summary> /// Configures the pipeline to use the optimal resolutions for VS based on the settings currently in use /// </summary> /// <returns></returns> private async Task SetUpVideoStabilizationRecommendationAsync() { Debug.WriteLine("Setting up VS recommendation..."); // Get the recommendation from the effect based on our current input and output configuration var recommendation = _videoStabilizationEffect.GetRecommendedStreamConfiguration(_mediaCapture.VideoDeviceController, _encodingProfile.Video); // Handle the recommendation for the input into the effect, which can contain a larger resolution than currently configured, so cropping is minimized if (recommendation.InputProperties != null) { // Back up the current input properties from before VS was activated _inputPropertiesBackup = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoRecord) as VideoEncodingProperties; // Set the recommendation from the effect (a resolution higher than the current one to allow for cropping) on the input await _mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoRecord, recommendation.InputProperties); Debug.WriteLine("VS recommendation for the MediaStreamProperties (input) has been applied"); } // Handle the recommendations for the output from the effect if (recommendation.OutputProperties != null) { // Back up the current output properties from before VS was activated _outputPropertiesBackup = _encodingProfile.Video; // Apply the recommended encoding profile for the output, which will result in a video with the same dimensions as configured // before VideoStabilization was added if an appropriate padded capture resolution was available. Otherwise, it will be slightly // smaller (due to cropping). This prevents upscaling back to the original size, which can result in a loss of quality _encodingProfile.Video = recommendation.OutputProperties; Debug.WriteLine("VS recommendation for the MediaEncodingProfile (output) has been applied"); } } 。代码不应该在浏览器中运行,因为它不是javascript。相反,它是java。

答案 3 :(得分:0)

您有多个错误;

只需使用这些;

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

要获得数组的长度,长度就足够了,你不必把它放在paranthesis上。此外,当您指定单词数组的 i 第char个元素时, 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;
    }

}

希望它有所帮助。