java实现键盘录入一个字符串,统计该字符串中大写字母和小写字母数字字符空格出现的次数

先说一下ASCII编码

	 * 大写字母A-Z (ASCII)65-90
     * 小写字母a-z (ASCII)97-122
     * 数字是:(ASCII)48-57
     * 空格的(ASCII)是32

接下来就是代码了,代码有标注的

import java.util.Scanner;

public class Test02 {
    public static void main(String[] args) {
     
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入大写字符");
        String str = scanner.nextLine();
        //定义三个变量,定义大写字母,小写字母,数字 字符出现的次数
        int bigCount=0;//大写
        int smallCount=0;//小写
        int number=0;//数字
        int Space=0;//空格
        //遍历字符串
        for (int i=0;i<str.length();i++){
            //取出遍历的字符
            char ch = str.charAt(i);
            //判断该字符是 大写字母,小写字母,数字,字符
            if (ch>=65&&ch<=90){
                bigCount++;
            }
            if (ch>=97&&ch<=122){
               smallCount++;
            }
            if (ch>=48&&ch<=57){
                number++;
            }
            if (ch==32){
                Space++;
            }
        }
        System.out.println("大写字母出现的次数为:"+bigCount);
        System.out.println("小写字母出现的次数为:"+smallCount);
        System.out.println("数字字母出现的次数为:"+number);
        System.out.println("空格出现的次数为:"+Space);
    }
}

你可能感兴趣的:(java,开发语言,算法)