本帖最后由 danielzyj 于 2014-4-21 16:36 编辑
- import java.util.Scanner;
- /*
- 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
- */
- class StrDemo
- {
- public static void main(String[] args)
- {
- int ZiMuNum=0;
- int KongGeNum=0;
- int ShuZiNum=0;
- int QiTaNum=0;
- Scanner sc = new Scanner(System.in);
- StringBuffer Str = new StringBuffer(sc.nextLine());
- for (int i = 0;i<=Str.length() ; i++)
- {
- if (Str.codePointAt(i)==32)
- {
- KongGeNum++;
- }
- else if(Str.codePointAt(i)>47&&Str.codePointAt(i)<58)
- {
- ShuZiNum++;
- }
- else if (Str.codePointAt(i)>64&&Str.codePointAt(i)<123)
- {
- ZiMuNum++;
- }
- else
- {
- QiTaNum++;
- }
- }
- System.out.print("字母个数"+ZiMuNum+"空格个数"+KongGeNum+"数字个数"+ShuZiNum+"其他字符个数"+QiTaNum);
- }
- }
复制代码
|
|