1.需求

统计元音字母——输入一个字符串,统计处其中元音字母的数量。更复杂点的话统计出每个元音字母的数量。

2.思路

输入:不超过100个字符的字符串。比如:"love me love my dog"。

处理:元音字母就a/e/i/o/u五个,可以分别统计出各自的数量,总数相加就可以了。

输出如下:

元音总次数:6
a次数:0
e次数:3
i次数:0
o次数:3
u次数:0

3.代码

package com.myeclipse;

public class VowelCount {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "love me love my dog";
        int[] counts = getVowelCount(str);
        System.out.println("元音总次数:"+counts[0]);
        System.out.println("a次数:"+counts[1]);
        System.out.println("e次数:"+counts[2]);
        System.out.println("i次数:"+counts[3]);
        System.out.println("o次数:"+counts[4]);
        System.out.println("u次数:"+counts[5]);
    }

    /**
     * 统计元音字母出现次数
     * @param str 不超过100个字符
     * @return
     */
    public static int[] getVowelCount(String str){
        //创建一个数组,分别存储元音字母出现的总次数和a/e/i/o/u分别出现的次数
        int[] voweCount = new int[6];
        
        for(int i=0; i<str.length(); i++) {
            char tmp = str.charAt(i);
            switch(tmp) {
            case 'a':voweCount[1]++;continue;
            case 'e':voweCount[2]++;continue;
            case 'i':voweCount[3]++;continue;
            case 'o':voweCount[4]++;continue;
            case 'u':voweCount[5]++;continue;
            }
        }
        for(int i=1; i<voweCount.length; i++){
            voweCount[0] += voweCount[i];
        }
        
        return voweCount;
    }
}
统计元音字母次数

相关文章:

  • 2022-01-18
  • 2022-12-23
  • 2022-12-23
  • 2021-11-02
  • 2022-12-23
  • 2022-12-23
  • 2021-07-21
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-17
  • 2022-12-23
  • 2022-12-23
  • 2021-07-14
  • 2021-10-29
相关资源
相似解决方案