public class Count {
public static void main(String[] args){
System.out.println("请输入要判断的字符串:");
Scanner sc = new Scanner(System.in);
//也可以使用String s=sc.nextLine(),区别在于这个啥都要,比如空格
String s = sc.next();
//转化成数组便于索引
char[] r = s.toCharArray();
int number = 0;
int Cword = 0;
int word = 0;
for (int i = 0; i <s.length(); i++) {
//或者直接使用 char ch = s.charAt(i),返回索引值处的字符
int num = (int) r[i];
//除了用ASCII码,也可以直接比较
//ch>='0' && ch<='9'
if(47<=num && num<=58){
number+=1;
}else if(num>=97 &&num<=122){
word+=1;
}else{
Cword+=1;
}
}
System.out.println("数字个数为:"+number);
System.out.println("小写字母个数为:"+word);
System.out.println("大写字母个数为:"+Cword);
}
}
Java实现判断输入中的数字、大小写字母的个数
最新推荐文章于 2024-04-08 10:14:11 发布