- package cn.itcast.heima;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class AlphaSum {
- public static void main(String[] args) {
- System.out.println("请输入一行字符:");
- //此处用bufferedReader接受屏幕输入
- BufferedReader std=new BufferedReader(new InputStreamReader(System.in));
- String str = null;
- //number数组用于接受各种字符个数
- int[] number = new int[4];
- //strs数组用于和number数组配合输出
- String[] strs = {"字母个数:","空格个数:","数字个数:","其他字符个数"};
- try {
- //读入一句
- str = std.readLine();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //此函数计算各个字符个数
- getNum(number,str);
- for(int i = 0;i < number.length;i++){
- System.out.println(strs[i] + number[i]);
- }
- }
-
- public static void getNum(int[] number,String str){
- byte[] bytes = str.getBytes();
- for(byte b : bytes){
- if((b >= 65 && b <= 90) || (b >= 97 && b <= 122)){
- number[0] ++;
- }else if(b == 32){
- number[1] ++;
- }else if(b >= 48 & b <= 57){
- number[2]++;
- }else{
- number[3]++;
- }
- }
- }
-
-
- }
复制代码 |