发个计算字符中空格,字母,数字个数的java类

bobo364 2009-12-01 11:25:52
今天天不错,昨天有人发的面试题中又有这样的问题,所以发个计算字符中空格,字母,数字个数的java类,水平次,如有不足,望指出,顺便撒20分

public class StringCaculate
{
private int i,j,k,l;
public StringCaculate
{
i=0;j=0;k=0;l=0;
}
public void caculate(String o)
{
StringBuffer aBuffer=new StringBuffer(o.lengh());
char aCharacter;
for(int m=0;m<o.lengh();m++)
{
aCharacter=o.charAt(i);
if(Character.isLetter(aCharacter))
i++;
else if(Character.isDigit(aCharacter))
j++;
else
l++;
}
System.out.println("有字母"+i+"个");
System.out.println("有数字"+j+"个");
System.out.println("有空格"+l+"个");
}
}



public class test
{
public static void main(String[] args)
{
String a="this is a string";
StringCaculate.caculate(a);
}
}



...全文
224 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
poikill 2009-12-01
  • 打赏
  • 举报
回复
学习学习
bobo364 2009-12-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 shine333 的回复:]
大哥,不知道你有没有测试过
Java codeStringCaculate sc=new StringCaculate();
sc.caculate("abc");
sc.caculate("abc");
sc.caculate("abc");
i,j,k没必要做成成员变量
[/Quote]

公司里没java,无法测试
bobo364 2009-12-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kf156 的回复:]
LZ貌似没考虑到字符串除了可以有空格、字母、数字外,还能有符号或其他文字
[/Quote]

仅限于判断这3者
shine333 2009-12-01
  • 打赏
  • 举报
回复
大哥,不知道你有没有测试过
StringCaculate sc = new StringCaculate();
sc.caculate("abc");
sc.caculate("abc");
sc.caculate("abc");

i,j,k没必要做成成员变量
kf156 2009-12-01
  • 打赏
  • 举报
回复
LZ貌似没考虑到字符串除了可以有空格、字母、数字外,还能有符号或其他文字
shine333 2009-12-01
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 bobo364 的回复:]
Java code/*
* To change this template, choose Tools | Templates
* and open the template in the editor.*/package exp1;/**
*
*@author 364*/publicclass StringCaculate
{privateint i,j,k,l;public StringCaculate()
{
i=0;j=0;k=0;l=0;
}publicvoid caculate(String o)
{
StringBuffer aBuffer=new StringBuffer(o.length());char aCharacter;for(int m=0;m<o.length();m++)
{
aCharacter=o.charAt(m);if(Character.isLetter(aCharacter))
i++;elseif(Character.isDigit(aCharacter))
j++;else
l++;
}
System.out.println("有字母"+i+"个");
System.out.println("有数字"+j+"个");
System.out.println("有空格"+l+"个");
}
}/*
* To change this template, choose Tools | Templates
* and open the template in the editor.*/package exp1;/**
*
*@author bobo364*/class test
{publicstaticvoid main(String[] args)
{
String a="this is a string";
StringCaculate c=new StringCaculate();
c.caculate(a);
}
}

回家用netbean修改测试通过的版本,顺便关帖
[/Quote]

LZ,还是每次有测试连续调用!
c.caculate(a);
c.caculate(a);
c.caculate(a);
bobo364 2009-12-01
  • 打赏
  • 举报
回复


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package exp1;

/**
*
* @author 364
*/
public class StringCaculate
{
private int i,j,k,l;
public StringCaculate()
{
i=0;j=0;k=0;l=0;
}
public void caculate(String o)
{
StringBuffer aBuffer=new StringBuffer(o.length());
char aCharacter;
for(int m=0;m<o.length();m++)
{
aCharacter=o.charAt(m);
if(Character.isLetter(aCharacter))
i++;
else if(Character.isDigit(aCharacter))
j++;
else
l++;
}
System.out.println("有字母"+i+"个");
System.out.println("有数字"+j+"个");
System.out.println("有空格"+l+"个");
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package exp1;

/**
*
* @author bobo364
*/
class test
{
public static void main(String[] args)
{
String a="this is a string";
StringCaculate c=new StringCaculate();
c.caculate(a);
}
}


回家用netbean修改测试通过的版本,顺便关帖
whut0802 2009-12-01
  • 打赏
  • 举报
回复
jf闪人
  • 打赏
  • 举报
回复
一般这种程序都会加一个条件就是大数据量,从文件中读取等等,按才是关键所在。
lovepay1413 2009-12-01
  • 打赏
  • 举报
回复
全角。。。。半角。。。
shine333 2009-12-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 applerockhool 的回复:]
对不?
Java codepublicstaticvoid testCount(String str) {char[] array= str.toCharArray();int letter=0, number=0, space=0;for (int i=0; i< array.length; i++) {if (array[i]=='')
space++;elseif (array[i]>='0'&& array[i]<='9')
number++;elseif (array[i]>='a'&& array[i]<='z')
letter++;
}
System.out.println("space="+ space+" number="+ number+" letter="+ letter);

}
[/Quote]
如果你只针对小写Ascii码
applerockhool 2009-12-01
  • 打赏
  • 举报
回复
对不?
public static void testCount(String str) {
char[] array = str.toCharArray();
int letter = 0, number = 0, space = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == ' ')
space++;
else if (array[i] >= '0' && array[i] <= '9')
number++;
else if (array[i] >= 'a' && array[i] <= 'z')
letter++;
}
System.out.println("space=" + space + " number=" + number + " letter="
+ letter);

}
Kevin2305 2009-12-01
  • 打赏
  • 举报
回复
lz肯定没有运行过,length都拼错了

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧