java统计英文字母、空格、数字和其它字符的数目

 1 package tes;
 2 
 3 import java.util.Scanner;
 4 
 5 //java统计英文字母,空格,数字和其它字符的数目
 6 public class ZiFuTongJi {
 7     public static void main(String[] args) 
 8         {
 9             Scanner sc = new Scanner(System.in);
10             System.out.println("输入字符串:");
11             int letterCount = 0; 
12             // 英文字母个数
13             int blankCount = 0; 
14             // 空格个数
15             int numCount = 0; 
16             // 数字个数
17             int otherCount = 0; 
18             // 其他字符个数
19             String str = sc.nextLine();
20             for (int i = 0; i < str.length(); i++) 
21             {// 通过指针转向,指向了str中的字符实现
22                 char item = str.charAt(i);
23                 if (item >= 'a' && item <= 'z' || item >= 'Z' && item <= 'A') 
24                 {
25                     letterCount++;
26                     } 
27                 else if (item == ' ') 
28                 {
29                     blankCount++;
30                     } 
31                 else if (item >= '0' && item <= '9')
32                 {numCount++;}
33                 else {
34                     otherCount++;
35                     }
36                 }
37                 System.out.println("英文" + letterCount);
38                 System.out.println("空格" + blankCount);
39                 System.out.println("数字" + numCount);
40                 System.out.println("其他" + otherCount);
41                 sc.close();
42                 }
43 }
原文地址:https://www.cnblogs.com/dgwblog/p/7635218.html