import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入字符:");
String c= scanner.nextLine();
char[] str=c.toCharArray();
int abc,blank,num,other;
abc=blank=num=other=0;
for (int i = 0; i < str.length; i++){
if(str[i]>='A'&&str[i]<='z')
abc++;
else if(str[i]>='0'&&str[i]<='9')
num++;
else if(str[i]==' ')
blank++;
else
other++;
}
System.out.println("这段字符串中英文字母:"+abc+"个,空格:"+blank+"个,数字:"+num+"个,其他字符:"+other+"个。");
}
}
示例输出

本文介绍了一段Java代码,该代码通过用户输入接收一串字符,并统计其中的英文字母、空格、数字和其他字符的数量。代码首先使用Scanner类读取用户输入,然后将字符串转换为字符数组进行遍历,利用条件判断统计不同类型的字符数量,最后输出统计结果。
9901

被折叠的 条评论
为什么被折叠?



