嘿嘿,我改的,不知道符合不符合你的要求- <p>package mypakage;</p><p>//输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- public class Demo
- {
- public static void main(String args[]) throws IOException
- {
- String s;
- int digital = 0;
- int character = 0;
- int other = 0;
- int blank = 0;
- //byte[] b=new byte[100];
- //创建字节写入流
- InputStreamReader isr=new InputStreamReader(System.in);//与输入设备相关联
- //创建缓冲区
- BufferedReader br=new BufferedReader(isr);
- //用s存入读取到的数据
- s=br.readLine();
- // br.close();
- //首先创建字符串
- //String str="3sf ad4a,ff,w5rw fwfw78fs";
- //将字符串转换为字符数组
-
- char[] ch =s.toCharArray();
-
- //Scanner sc = new Scanner(System.in);
-
- //String s = sc.nextLine();
-
- //ch = s.toCharArray();
-
- for(int i=0; i<ch.length;i++)
- {
-
- if(ch[i]>= '0' && ch[i] <= '9')
- {
-
- digital++;
-
- }
- else if((ch[i]>= 'a' && ch[i]<= 'z') || ch[i] > 'A' && ch[i] <= 'Z')
- {
-
- character ++;
-
- }
- else if(ch[i] == ' ')
- {
-
- blank ++;
-
- }
- else
- {
-
- other ++;
-
- }
-
- }
-
- System.out.println("数字个数: " + digital);
-
- System.out.println("英文字母个数: " + character);
-
- System.out.println("空格个数: " + blank);
-
- System.out.println("其他字符个数:" + other );
- }
- }
- </p><p> </p>
复制代码 |