java编写程序,读入用户输入的一个字符串,然后确定并输出每一个小写元音字母(a,e,i,o,u)在

整个字符串中出现的次数。每一个元音字母用不同的计数器统计,同时也输出非元音字母的个数。

import java.util.Scanner;

public class StringDemo {
static char[] cs = {'a','e','i','o','u'};
static char[] bcs = {'A','E','I','O','U'};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int[] arys = findChar(str);
for (int i = 0; i <cs.length; i++) {
System.out.println(cs[i]+"出现的个数"+arys[i]);
}
System.out.println("大写元音字母的个数"+arys[5]);
System.out.println("非元音字母的个数"+arys[6]);
}

private static int[] findChar(String str) {
int[] arys = new int[7];//0~5存储每个小写元音的个数,6存储大写元音的个数,7存储非元音的个数
int sumx= 0;//用于保存元音字母的个数(不区分大小写)
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
for (int j = 0; j < cs.length; j++) {
if(c==cs[j]){
arys[j]++;//小写元音个数增加
sumx++;
break;
}
}
for (int j = 0; j < bcs.length; j++) {
if(c==bcs[j]){
arys[5]++;
sumx++;
break;
}
}
}
  arys[6] = str.length()-sumx;//非元音的个数
return arys;
}
}

输出

AppleIlove
a出现的个数0
e出现的个数2
i出现的个数0
o出现的个数1
u出现的个数0
大写元音字母的个数2
非元音字母的个数5

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-11-08
import java.util.*;
public class Yugi {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System. in );
        String line = scan.nextLine().trim();
        scan.close();
        HashMap < Character, Integer > map = new HashMap < Character, Integer > ();
        for (int i = 0; i < line.length(); i++) {
            char c = line.charAt(i);
            if (c >= 'a' && c <= 'z') {
                if (null == map.get(c)) {
                    map.put(c, 1);
                } else {
                    map.put(c, map.get(c) + 1);
                }
            }
        }
        System.out.println(map);
    }
}

本回答被网友采纳
第2个回答  2015-11-08
你是B班的。
相似回答