【文件属性】:
文件名称:统计字母空格数字和其它字符个数-devexpress中gridcontrol使用技巧总结-详解(图文并茂)
文件大小:4.48MB
文件格式:PDF
更新时间:2021-06-08 16:50:44
java 入门资料 java入门资料
2.9 统计字母、空格、数字和其它字符个数
2.9.1 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
2.9.2 源程序
import java.util.*;
public class Number {
static int digital = 0;
static int character = 0;
static int other = 0;
static int blank = 0;
public static void main(String[] args) {
char[] ch = null;
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
if (ch[i] >= '0' && ch[i] <= '9') {
digital++;
} else if ((ch[i] >= 'a' && ch[i] <= 'z') || ch[i] > 'A'
&& ch[i] <= 'Z') {
character++;
} else if (ch[i] == ' ') {
blank++;
} else {
other++;
}
}
System.out.println("数字个数: " + digital);
System.out.println("英文字母个数: " + character);
System.out.println("空格个数: " + blank);
System.out.println("其他字符个数:" + other);
}
}
2.9.3 运行结果:
sadf239 asl!~@#*(#)