首页 > 其他分享 >For

For

时间:2024-04-06 22:59:32浏览次数:14  
标签: Scanner int currentChar && stringLength myString

import java.util.Scanner;

public class For {
    public static void main(String args[]){
        String myString = null;
        int stringLength = 0;
        int letterNum = 0;
        int decimalNum = 0;
        int otherNum = 0;
        char currentChar;
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入字符串:");
        myString = scan.nextLine();
        stringLength = myString.length();
        for (int i = 0;i < stringLength; i++) {
            currentChar = myString.charAt(i);
            if (currentChar >= '0' && currentChar <= '9')
                decimalNum++;
            else if ((currentChar >= 'a' && currentChar <= 'z') || (currentChar >= 'A' && currentChar <= 'Z'))
                letterNum++;
            else
                otherNum++;
        }
        System.out.println("输入的字符串:" + myString);
        System.out.println("字母个数:" + letterNum);
        System.out.println("数字个数:" + decimalNum);
        System.out.println("其他类型字符个数:" + otherNum);
        scan.close();
    }
}

标签:,Scanner,int,currentChar,&&,stringLength,myString
From: https://blog.csdn.net/2301_80431225/article/details/137440872

相关文章