一个java程序问题,我是新手不知道怎么写。

题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。一个java题,求大神讲解。

用正则表达式。匹配一下下。

用一个for循环,检查输入的每一个字符,字符的ASCII值在48到57的为数字,在65到90的为大写字母,在97到122的为小写字母,等于32的为空格

public static void main(String[]args){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一串字符串");
String string = scanner.nextLine();
int letter=0;
int number=0;
int blank=0;
int other=0;
for(int i=0;i {
if((string.charAt(i)>=65&&string.charAt(i)<=90)||(string.charAt(i)>=97&&string.charAt(i)<=122))
letter++;
else if(string.charAt(i)>48&&string.charAt(i)<=57)
number++;
else if(string.charAt(i)==32)
blank++;
else
other++;
}
System.out.println("有"+letter+"个字母,"+number+"个数字,"+blank+"个空格,"+other+"个其他字符.");
}

    两个需要注意的地方
    第一个,String string = scanner.nextLine();如果使用next()获取字符串的话,当输入空格的时候,不会占一个位置(相当于判断你没输入)。
    第二个,那个判断的地方是根据ASCII码表来判断的,你可以自行百度一下ASCII码表就懂得为什么是65,90等等了
    当然还可以通过正则表达式去匹配等方法

一楼已经描述的很详细了,自古一楼出正解啊

一楼已经描述的很详细了