- import java.util.*;
- public class CountNum {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- System.out.print("请输入一行字符:");
- String str = in.nextLine();
- count_num(str);
- }
- public static void count_num(String str)
- {
- int ZiMu = 0; //英文字母的个数
- int KongGe = 0; //空格的个数
- int ShuZi = 0; //数字的个数
- int other = 0; //其他字符的个数
- for(int i=0;i<str.length();i++)
- {
- char ch = str.charAt(i);
- if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){
- ZiMu++;
- }
- else if(ch==' '){
- KongGe++;
- }
- else if(ch>='0'&&ch<='9'){
- ShuZi++;
- }
- else{
- other++;
- }
- }
- System.out.println("字母的个数:"+ZiMu);
- System.out.println("空格的个数:"+KongGe);
- System.out.println("数字的个数:"+ShuZi);
- System.out.println("其他字符的个数:"+other);
- }
- }
复制代码
|