Java编程:输入一行字符统计其中数字,大小写字母、空格和其他字符的个数

import java.util.Scanner;

class Stat_char{
	int number = 0,capital_letter = 0, lowercase = 0 ,blank = 0, rests_char = 0;
	Scanner in = new Scanner(System.in);
	public void show()
	{	
		System.out.print("请输入一串字符串统计其中字符的个数:");
		String c1 = in.nextLine();
		char []a = c1.toCharArray();
		for(int i = 0 ; i<a.length ; i++)
		{
			if(a[i]>='0'&&a[i]<='9')
			{
				number++;
			}else if(a[i]>='A'&&a[i]<='Z')
			{
				capital_letter++;
			}else if(a[i]>='a'&&a[i]<='z')
			{
				lowercase++;
			}else if(a[i]==' ')
			{
				blank++;
			}else
			{
				rests_char++;
			}
		}
	}
	public void print()
	{
		System.out.println("数字有:"+number+"个");
		System.out.println("大写字母有:"+capital_letter+"个");
		System.out.println("小写字母有:"+lowercase+"个");
		System.out.println("空格有:"+blank+"个");
		System.out.println("其他字符有:"+rests_char+"个");
	}
}
public class Sample_2_22 {

	public static void main(String[] args) {
		Stat_char ch = new Stat_char();
		ch.show();
		ch.print();
	}

}

阅读更多
文章标签: java
个人分类: Java基础练习
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭