Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create 统计元音字母个数和每个元音字母出现的个数 #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

blench
Copy link

@blench blench commented May 27, 2017

`package interesting;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
*

  • @author Administrator
    *统计元音字符出现的总数,还有每个元音字符出现的个数
    */
    public class CountChar {

    int count = 0;
    char[] ch = null;
    int[] countCh = null;

    Map<Character,Integer> hashMap = new HashMap();
    public void countVowel(String s)
    {

     if(s.length()>0 && s!=null)
     {
     	s = s.toLowerCase();
     	ch = new char[s.length()];
     	countCh = new int[s.length()];
     	for(int i=0;i<s.length();i++)
     	{
     		
     		if(isVowel(s.charAt(i)))
     		{
     			ch[i] = s.charAt(i);
     			count++;
     			countCh[i] =1; 
     		}
     	}
     	
     
     	//避免重复打印相同元音字符的情况
     	for(int i=0;i<ch.length;i++)
     	{
     		if(hashMap.containsKey(ch[i]))
     		{
     			Integer value = hashMap.get(ch[i]);
     			value++;
     			hashMap.put(ch[i], value);
     		}
     		else 
     		{
     			//这里要注意了,还包括其他非元音字母,所以要做一下判断
     			if(isVowel(ch[i]))
     			{
     				hashMap.put(ch[i], 1);
     			}
     		}
     	}
     }
    

    }

    /*

    • 判断某个字符是否是元音字符

    */
    public boolean isVowel(char c)
    {
    if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u')
    {
    return true;
    }
    return false;
    }

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    String s = input.nextLine();
    CountChar count = new CountChar();
    count.countVowel(s);
    System.out.println("元音出现的总数为: "+count.count);
    System.out.println("每个元音字符出现的个数为: "+count.hashMap);
    }

}

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant