黑马程序员技术交流社区
标题:
一道关于统计循环的问题,求解惑 ---已解决
[打印本页]
作者:
王健
时间:
2012-8-12 20:04
标题:
一道关于统计循环的问题,求解惑 ---已解决
本帖最后由 王健 于 2012-8-13 00:22 编辑
输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
作者:
巩彪
时间:
2012-8-12 20:10
你看看这个行不行 分析写的有些草率 要是有不懂的 再问我
首先 程序分析:利用while语句,条件为输入的字符不为'\n'.
正题如下:
import java.util.Scanner;
public class Prog7_1{
public static void main(String[] args){
System.out.print("请输入一串字符:");
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();//将一行字符转化为字符串
scan.close();
count(str);
}
//统计输入的字符数
private static void count(String str){
String E1 = "[\u4e00-\u9fa5]";//汉字
String E2 = "[a-zA-Z]";
String E3 = "[0-9]";
String E4 = "\\s";//空格
int countChinese = 0;
int countLetter = 0;
int countNumber = 0;
int countSpace = 0;
int countOther = 0;
char[] array_Char = str.toCharArray();//将字符串转化为字符数组
String[] array_String = new String[array_Char.length];//汉字只能作为字符串处理
for(int i=0;i<array_Char.length;i++)
array_String[i] = String.valueOf(array_Char[i]);
//遍历字符串数组中的元素
for(String s:array_String){
if(s.matches(E1))
countChinese++;
else if(s.matches(E2))
countLetter++;
else if(s.matches(E3))
countNumber++;
else if(s.matches(E4))
countSpace++;
else
countOther++;
}
System.out.println("输入的汉字个数:"+countChinese);
System.out.println("输入的字母个数:"+countLetter);
System.out.println("输入的数字个数:"+countNumber);
System.out.println("输入的空格个数:"+countSpace);
System.out.println("输入的其它字符个数:"+countSpace);
}
}
import java.util.*;
public class Prog7_2{
public static void main(String[] args){
System.out.println("请输入一行字符:");
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
scan.close();
count(str);
}
//统计输入的字符
private static void count(String str){
List<String> list = new ArrayList<String>();
char[] array_Char = str.toCharArray();
for(char c:array_Char)
list.add(String.valueOf(c));//将字符作为字符串添加到list表中
Collections.sort(list);//排序
for(String s:list){
int begin = list.indexOf(s);
int end = list.lastIndexOf(s);
//索引结束统计字符数
if(list.get(end)==s)
System.out.println("字符‘"+s+"’有"+(end-begin+1)+"个");
}
}
}
作者:
来明坤
时间:
2012-8-12 20:10
这个好多论坛还有博客有例子的 方法也不只一种 建议你再看看有没有更容易理解的 或更简单的{:soso__8961432591078930798_3:}
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
int countNum = 0;//统计数字的个数
int countChar = 0;//统计英文字母的个数
int countSpace = 0;//统计空格的个数
int countOthers = 0;//统计其它字符的个数
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c >= '0' && (int) c <= '9') {
countNum++;
} else if ((c >= 'a' && c <= 'z')||(c >= 'A' && c <= 'Z')) {
countChar++;
} else if (c == ' ') {
countSpace++;
} else{
countOthers++;
}
}
System.out.println("数字个数:"+countNum);
System.out.println("英文字母个数:"+countChar);
System.out.println("空格个数:"+countSpace);
System.out.println("其他字符个数:"+countOthers);
}
作者:
张忠豹
时间:
2012-8-12 20:59
版主的小弟也很牛叉啊!
public static void main(String[] args) throws IOException {
// 根据字符的ASCII查找
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
StringBuffer sb = new StringBuffer();
sb.append(str);
int a = 0, b = 0, c = 0;
for (int j = 0; j < sb.length(); j++) {
//48——57代表0——9
if (sb.charAt(j) <= 57 & sb.charAt(j) >= 48) {
a++;
}
//65——90代表A-Z,97-122代表a——z
if (sb.charAt(j) <= 90 & sb.charAt(j) >= 65 | sb.charAt(j) <= 122
& sb.charAt(j) >= 97) {
b++;
}
if (sb.charAt(j) == ' ') {
c++;
}
}
System.out.println("数字个数:" + a);
System.out.println("字母数:" + b);
System.out.println("空格数:" + c);
System.out.println("其他字符数:" + (sb.length() - a - b - c));
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2