数字范围为0~9
小写字母范围为 a~z
大写字母范围为 A~Z
package com.itheima.test7;
import java.util.Scanner;
public class CountNumber {
public static void main(String[] args) {
// 键入一串字符串,并统计一个字符串中字母、数字分别为多少
// 1.键入
Scanner sc=new Scanner(System.in);
System.out.println("请输入字符串");
String s=sc.nextLine();
// 2.统计字符串中字母和数字
// 记录数字个数
int countNumber=0;
// 记录字母个数
int countEnglish=0;
// 遍历字符串
for(int i=0;i<s.length();i++){
// 接收字符
char ch=s.charAt(i);
// 统计数字
if(ch>='0'&&ch<='9'){
countNumber++;
}
// 统计字母
else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){
countEnglish++;
}
}
// 3.输出
System.out.println("一共有"+countNumber+"个数字");
System.out.println("一共有"+countEnglish+"个字母");
}
}