java中字符统计试题示例
2012-08-13 13:58
281 查看
输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
import java.util.*;
public class lianxi07 {
public static void main(String[] args) {
int digital = 0;
int character = 0;
int other = 0;
int blank = 0;
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 >= '0' && ch <= '9') {
digital ++;
} else if((ch >= 'a' && ch <= 'z') || ch > 'A' && ch <= 'Z') {
character ++;
} else if(ch == ' ') {
blank ++;
} else {
other ++;
}
}
System.out.println("数字个数: " + digital);
System.out.println("英文字母个数: " + character);
System.out.println("空格个数: " + blank);
System.out.println("其他字符个数:" + other );
}
}
import java.util.*;
public class lianxi07 {
public static void main(String[] args) {
int digital = 0;
int character = 0;
int other = 0;
int blank = 0;
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 >= '0' && ch <= '9') {
digital ++;
} else if((ch >= 'a' && ch <= 'z') || ch > 'A' && ch <= 'Z') {
character ++;
} else if(ch == ' ') {
blank ++;
} else {
other ++;
}
}
System.out.println("数字个数: " + digital);
System.out.println("英文字母个数: " + character);
System.out.println("空格个数: " + blank);
System.out.println("其他字符个数:" + other );
}
}
相关文章推荐
- Java统计字符串中字符出现次数的方法示例
- java实现的统计字符算法示例
- java实现统计字符串中字符及子字符串个数的方法示例
- 【Java学习之代码学习】 Prog12_统计字符个数
- 字符统计1(java)
- java统计字符串中字符及子字符串个数
- Java 统计字符个数和分离字符串
- java源码——统计字符串中字符出现的次数
- 华为机试题:去除数字后一个重复的字符 java
- java正则匹配HTML中a标签里的中文字符示例
- JS实现统计字符串中字符出现个数及最大个数功能示例
- 字符统计,字符串查询,java
- java例程练习(统计字母数字等字符的个数)
- Java之集合常见例题(1、统计字符出现次数2、集合嵌套遍历。3、斗地主)
- java统计字符串中重复【单个】字符的次数频次-使用字符数组实现
- Introduction to Java Programming编程题9.17<统计文件行、字符、单词数>
- Java - 华为机试训练 - 字符个数统计
- java给定一串字符串,统计其中的各个字符的出现的次数
- java统计字符串中每个字符出现的次数
- java例程练习(统计字母数字等字符的个数)