要求:输入一串字符,分别统计英文字母,数字,空格和其他字符的个数。(最好用DO-WHILE结构)
这是我写的代码:
import java.io.*;public class Count {
public static void main(String[] args) throws IOException{
(new CountChar()).count();
}
}
class CountChar {
public void count() throws IOException{
char ch;
int m=0;
int n=0;
int o=0;
int p=0;
System.out.println("please input string");
do
{
ch=(char)System.in.read();
if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z'){
m+=1;}
else if(ch>='0'&&ch<='9'){
n+=1;}
else if(ch==' '){
o+=1;}
else {
p+=1;}
}while(ch!='\n');
System.out.println("英语字母有"+m+"个");
System.out.println("数字有"+n+"个");
System.out.println("空格有"+o+"个");
System.out.println("其他字符有"+p+"个");
}
}
问题:统计其他字符的个数总是不对,如何判断是不是特殊字符呢?
请各位指点!有更好的代码页可以贴出来!
这是我写的代码:
import java.io.*;public class Count {
public static void main(String[] args) throws IOException{
(new CountChar()).count();
}
}
class CountChar {
public void count() throws IOException{
char ch;
int m=0;
int n=0;
int o=0;
int p=0;
System.out.println("please input string");
do
{
ch=(char)System.in.read();
if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z'){
m+=1;}
else if(ch>='0'&&ch<='9'){
n+=1;}
else if(ch==' '){
o+=1;}
else {
p+=1;}
}while(ch!='\n');
System.out.println("英语字母有"+m+"个");
System.out.println("数字有"+n+"个");
System.out.println("空格有"+o+"个");
System.out.println("其他字符有"+p+"个");
}
}
问题:统计其他字符的个数总是不对,如何判断是不是特殊字符呢?
请各位指点!有更好的代码页可以贴出来!
解决方案 »
- 请帮看一个字符串相似度分析的bug
- 重载解析、动态绑定、可协变返回类型的研究
- 一个简单问题
- 一个脚本问题 图片滚动的
- 为什么我用IP访问, 怎么之后运行的程序中运行时得到的是 主机名?
- 哪位大哥可以提供陈孝祥的JAVA视频教程.
- xml高手进入,如何将Document对象变成String类型的xml呀
- 如何填充JCheckBox的颜色,用setBackground()也不行!
- 有两个Date对象,如何取得它们的差? 要求分别返回年,月,日,时,分,秒?
- Syntax error on token "class", @ expected大神们这为什么报错哇
- 求一个 synchronized 问题
- UDP编程出现java.io.IOException: Invalid argument 异常
public class Count {
public static void main(String[] args) throws IOException {
(new CountChar()).count();
}
}class CountChar {
public void count() throws IOException {
char ch;
int m = 0;
int n = 0;
int o = 0;
int p = 0;
Map<String,HashSet<Character>> map = new HashMap<String,HashSet<Character>>();
System.out.println("please input string");
do {
ch = (char) System.in.read();
if(ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z'){
if(map.containsKey("英文字母")){
map.get("英文字母").add(ch);
}
else{
HashSet<Character> chs = new HashSet<Character>();
chs.add(ch);
map.put("英文字母", chs);
}
m++;
} else if (ch >= '0' && ch <= '9'){
if(map.containsKey("数字")){
map.get("数字").add(ch);
}
else{
HashSet<Character> chs = new HashSet<Character>();
chs.add(ch);
map.put("数字", chs);
}
n += 1;
} else if (ch == ' ') {
if(map.containsKey("其他字符")){
map.get("其他字符").add(ch);
}
else{
HashSet<Character> chs = new HashSet<Character>();
chs.add(ch);
map.put("其他字符", chs);
}
o += 1;
}else{
if(map.containsKey("其他字符")){
map.get("其他字符").add(ch);
}
else{
HashSet<Character> chs = new HashSet<Character>();
chs.add(ch);
map.put("其他字符", chs);
}
System.out.println(ch=='\r'||ch=='\n');//你这里的其他字符就是这两个字符。没有统计错吧
p += 1;
}
} while (ch != '\n');
System.out.println("英语字母有" + m + "个");
System.out.println("数字有" + n + "个");
System.out.println("空格有" + o + "个");
System.out.println("其他字符有" + p + "个");
System.out.println(map); }
}
please input string
abc12 (*
false
false
true
true
英语字母有3个
数字有2个
空格有2个
其他字符有4个
{其他字符=[ , *,
, (,
], 数字=[2, 1], 英文字母=[b, c, a]}
查过JAVA文档,还是对HASHMAP,HASHSET没读懂什么意思。映射用吗?
其他字符为什么是4个,是不是还统计进了\r和\n?
TRUE和FALSE的输出结果怎么出来的?
其他字符统计了结束符"\r\n"两个字符,加上你的*,(就是4个了。
System.out.println(ch=='\r'||ch=='\n');//你这里的其他字符就是这两个字符。没有统计错吧
当你输入*,(,不是匹配'\r','\n',所以前两个是false,后两个是true
其实我写这句话的意思是解释那个其他字符为什么是2。
还有就是我是否可以把回车换行从其他字符的统计中去掉。
ch = (char)(System.in).read();
if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z') {
m += 1;
} else if (ch >= '0' && ch <= '9') {
n += 1;
} else if (ch == ' ') {
o += 1;
} else if(ch!='\r'&&ch!='\n') {
p += 1 ;
} } while (ch != '\n');
MAP.CONTAINSKEY按字母意思理解就可以了
map是否包含这个key,钥匙,把map里面的元素当做1把钥匙对应一把锁。map.get(key),就是取key对应的锁。
当然可以把回车和换行取消掉了。
Scanner in = new Scanner(System.in);
String s = in.nextLine();
这里nextLine()会把换行符"\r\n"截掉的。
在统计s里面的字符
char[] cs = s.toCharArray();
这是一种办法。
还有就是上面你加个判断就想了
if(ch=='\r'||ch=='\n')不统计字符。