【程序7】 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

/*
    2017年3月2日16:47:24
    java基础50道经典练习题 例7
    Author:ZJY(&&)
    Purpose:对字符串的数据的类型计数
     【程序7】
		题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
		程序分析:利用while语句,条件为输入的字符不为'\n'.
*/
import java.util.Scanner;

public class ProgramNo7_1
{
	public static void main(String[] args)
	{
		System.out.print("请输入一串字符串数据: ");
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		sc.close();
		
		count(str);
	}
	private static void count(String str) {
		int countChinese = 0;
		int countLetter = 0;
		int countNumber = 0;
		int countSpace = 0;
		int countOther = 0;

		for (int i=0; i<str.length(); i++) {
			if(('a' <= str.charAt(i))&&('z' >= str.charAt(i)) //字母
				||('A' <= str.charAt(i))&&('Z' >= str.charAt(i))) {
				countLetter++;
			}else if(('0' <= str.charAt(i))&&('9' >= str.charAt(i))) { //数字
				countNumber++;
			}else if(' ' == str.charAt(i)) { //空格
				countSpace++;
			}else if((0x4e00 <= str.charAt(i))&&(0x9fa5 >= str.charAt(i))) { //汉字
				countChinese++;
			}else { //其他
				countOther++; 
			}
		}
		System.out.println("输入的字符串中字母有: " + countLetter + "个");
		System.out.println("输入的字符串中数字有: " + countNumber + "个");
		System.out.println("输入的字符串中空格有: " + countSpace + "个");
		System.out.println("输入的字符串中汉字有: " + countChinese + "个");
		System.out.println("输入的字符串中其他有: " + countOther + "个");
	}
}



/*
	2017年3月3日10:15:59
	java基础50道经典练习题 例7
	Athor: ZJY
	Purpose: 字符的分类计数
*/
import java.util.Scanner;

public class ProgramNo7_2
{
	public static void main(String[] args)
	{
		System.out.printf("请输入一串字符串: ");
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		sc.close();
		
		count(str);
	}
	private static void count(String str)
	{
	    String E1 = "[\u4e00-\u9fa5]"; //汉字范围
	    String E2 = "[a-zA-Z]"; //字母范围
		String E3 = "[0-9]"; //数字范围
		String E4 = "[ ]"; //空格
	    int countChinese = 0;
		int countLetter = 0;
		int countNumber = 0;
		int countSpace = 0;
		int countOther = 0;
		
		char[] arrayChar = str.toCharArray();
		String[] arrayString = new String[arrayChar.length]; 
		for (int i=0; i<arrayChar.length; i++)
			arrayString[i] = String.valueOf(arrayChar[i]);

		for (String str2: arrayString) { //意思将引用arrayString赋值给str2 
		//相当于for(int i=0; i<arrayString.length; i++)
			if(str2.matches(E1)) {
				countChinese++;
			}else if(str2.matches(E2)) {
				countLetter++;
			}else if(str2.matches(E3)) {
				countNumber++;
			}else if(str2.matches(E4)) {
				countSpace++;
			}else {
				countOther++;
			}
		}
		System.out.println("输入的字符串中字母有: " + countLetter + "个");
		System.out.println("输入的字符串中数字有: " + countNumber + "个");
		System.out.println("输入的字符串中空格有: " + countSpace + "个");
		System.out.println("输入的字符串中汉字有: " + countChinese + "个");
		System.out.println("输入的字符串中其他有: " + countOther + "个");
	}
}

/*
	2017年3月3日11:23:35
	java基础50道经典练习题 例7
	Athor: ZJY
	Purpose: 字符的分类计数
*/

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class ProgramNo7_3
{
	public static void main(String[] args)
	{
		System.out.printf("请输入一串字符串: ");
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		sc.close();
		
		count(str);
	}
	private static void count(String str)
	{
	    List<String> Lis = new ArrayList<String>();
		char[] arrayChar = str.toCharArray();
		for (char c: arrayChar)
			Lis.add(String.valueOf(c));
		
		Collections.sort(Lis); //排序  Set和List类型的容器都继承与Collection接口 
		
		for (String s: Lis) { //将线性List链表数组按顺序中去出泛型String元素放在s上
			int begin = Lis.indexOf(s);
			int end = Lis.lastIndexOf(s);
			
			//索引结束统计个字符数
			if(Lis.get(end) == s)
				System.out.println("字符" + s + "有" + (end-begin+1) + "个");
		}
		
	}
}





  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值