您的位置:首页 > 其它

输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

2009-06-25 14:35 561 查看
package cn.edu.hust.divisor;

import java.util.Scanner;

public class ReadChar {

public static void main(String[] args) {

Scanner stdin = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String str = stdin.nextLine();
new ReadChar().statString(str);
}

public void statString(String str) {
int strNum = 0;// 英文字符的个数
int spaceNum = 0;// 空格的个数
int numsNum = 0;// 数字的个数
int otherNum = 0;// 其他的个数
for (int index = 0; index < str.length(); index++) {
int num = (int) str.charAt(index);
if ((num >= 97 && num <= 122) || (num >= 65 && num <= 90)) {
strNum++;
} else if ((num >= 48 && num <= 57)) {
numsNum++;
} else if (num == 32) {
spaceNum++;
} else {
otherNum++;
}
}
{
System.out.println("您所输入的字符串为:" + str);
System.out.println("此字符串有英文字符:" + strNum + "个");
System.out.println("此字符串有数字:" + numsNum + "个");
System.out.println("此字符串有空格:" + spaceNum + "个");
System.out.println("此字符串有其他字符:" + otherNum + "个");
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐