输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。请教下各方高手,我还是个JAVA初学者,麻烦大家了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class ZhangZhenWei{ public static void main(String [] args) { String s = "zhangzhenwei"; int number = 0,smallchar = 0,bigchar = 0,other = 0; for(int i = 0;i < s.length();i++) { char n = s.charAt(i); if(n>='0'&&n<='9') number++; else if (n>='a'&&n<='z') smallchar++; else if (n>='A'&&n<='Z') bigchar++; else other++; } }}//输出我就不输出了,我也是初学者有问题我们可以讨论。 我来一个全的package com.fafeiboy.test;public class TestCount { private int i = 0; private int j = 0; private int k = 0; private int l = 0; private int m = 0; public void countChar(String s) { char[] arrayChar = s.toCharArray(); char c; for (int x = 0; x < arrayChar.length; x++) { c = arrayChar[x]; if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { //判断英文字母 i++; } else if (c >= '\u4e00' && c <= '\u9fa5') { //判断中文 j++; } else if (c == ' ') { //判断空格 k++; } else if (c >= '0' && c < '9') { //判断数字 l++; } else { m++; } } System.out.println("i:" + i); System.out.println("j:" + j); System.out.println("k:" + k); System.out.println("l:" + l); System.out.println("m:" + m); } public static void main(String[] args){ TestCount tc = new TestCount(); tc.countChar("asdfasdf中 a 121 \\[][23423.,/"); }} int main(){ char temp; while(getchar(temp)!='\n'){ if(number range){ numberNum++; }else if(charator range){ charactorNum++; }else if(space){ spaceNum++; }else { otherNum++; } } printf("Number:%d\nCharactor:%d\nSpace::%d\nOther:%d\n"); return 0; } 2楼的是答案,不过这个问题学C语言基础的时候相信大家都做过,算法还是那样。不过是用了java的特有的对象表现出来了,呵呵··· 一道简单面试题! java 双人俄罗斯方块按键控制 在线急求:Java怎么把数字字符型数组中的数字字符转为整型? 怎么样在java里实现在命令行使用自己命名的命令? 有关使用HttpClient的问题 线程为何挂起? ● 关于导入文件的奇怪的东东!! transient 关键字有什么用途 哪位大侠知道哪有java聊天室的源代码可下载,请告知!谢谢!;) Java怎么样求水仙花数呢 大家帮我看一下以下java代码中this都是指的什么? 谁有一个简单的邮件收发系统啊
{
public static void main(String [] args)
{
String s = "zhangzhenwei";
int number = 0,smallchar = 0,bigchar = 0,other = 0;
for(int i = 0;i < s.length();i++)
{
char n = s.charAt(i);
if(n>='0'&&n<='9') number++;
else if (n>='a'&&n<='z') smallchar++;
else if (n>='A'&&n<='Z') bigchar++;
else other++;
}
}
}//输出我就不输出了,我也是初学者有问题我们可以讨论。
package com.fafeiboy.test;public class TestCount {
private int i = 0; private int j = 0; private int k = 0; private int l = 0; private int m = 0; public void countChar(String s) {
char[] arrayChar = s.toCharArray();
char c;
for (int x = 0; x < arrayChar.length; x++) {
c = arrayChar[x];
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
//判断英文字母
i++;
} else if (c >= '\u4e00' && c <= '\u9fa5') {
//判断中文
j++;
} else if (c == ' ') {
//判断空格
k++;
} else if (c >= '0' && c < '9') {
//判断数字
l++;
} else {
m++;
}
}
System.out.println("i:" + i);
System.out.println("j:" + j);
System.out.println("k:" + k);
System.out.println("l:" + l);
System.out.println("m:" + m);
}
public static void main(String[] args){
TestCount tc = new TestCount();
tc.countChar("asdfasdf中 a 121 \\[][23423.,/");
}
}
char temp;
while(getchar(temp)!='\n'){
if(number range){
numberNum++;
}else if(charator range){
charactorNum++;
}else if(space){
spaceNum++;
}else {
otherNum++;
}
}
printf("Number:%d\nCharactor:%d\nSpace::%d\nOther:%d\n");
return 0;
}