集册 Java实例教程 元音字母计数

元音字母计数

欢马劈雪     最近更新时间:2020-01-02 10:19:05

418
元音字母计数

import java.util.*;


public class CountVowels {/*nowjava 提 供*/


    public static void main(String[] args) {


        if (args.length < 1) {

            System.err.println("Usage: java CountVowels <word1 word2 ...>");

            System.exit(1);

        }


        Map<String, Integer> msi = new HashMap<>();


        for (String s : args) {

            int vowelCount = 0;

            int length = s.length();
            /*
            来 自*
             时 代 J a v a
            */

            for (int i = 0; i < length; ++i)

                if (isVowel(s.charAt(i)))

                    ++vowelCount;


            msi.put(s, vowelCount);

        }


        Set<String> words = msi.keySet();

        for (String wd : words)

            System.out.println(wd + ": " + msi.get(wd));

    }


    public static boolean isVowel(char c) {

        return c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i'

                || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U';

    }

}


展开阅读全文