Java利用Scanner类,从键盘接受一个字符串输入,该字符串包含小写字母,大写字母和数字。分别输出该字符串所包含的大写字母、小写字母和数字的个数。

题目要求:利用Scanner类,从键盘接受一个字符串输入,该字符串包含小写字母,大写字母和数字。分别输出该字符串所包含的大写字母、小写字母和数字的个数。

import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {
        System.out.println("请输入一串字符串:");
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        int numbercount = 0;        // 数字的个数
        int bigCount = 0;         //  大写字母的个数
        int smallCount = 0;       //   小写字母的个数
        int otherCount = 0;       //   其它字符的个数
        for(int i =0,len=str.length();i<len;i++){
            char ch = str.charAt(i);
            if(ch >= 'A' && ch <= 'Z'){
                bigCount++;
            }else if (ch >= 'a' && ch <= 'z' ){
                smallCount++;
            }else if(ch >='0' && ch <='9'){
                numbercount++;
            }else{
                otherCount++;
            }
        }
        System.out.println("大写字母的个数:"+bigCount);
        System.out.println("小写字母的个数:"+smallCount);
        System.out.println("数字的个数:" +numbercount);
        System.out.println("其它字符的个数:"+otherCount);

    }
}

运行效果

猜你喜欢

转载自blog.csdn.net/Sunny_Boy0518/article/details/134103803
今日推荐