JAVA中怎么判断一个数组中所有元素的数据类型?

题目是这样的: 输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数,我是把这一行字符存到数组里,然后挨个判断,但是instanceof不行还有别的方法吗

import java.util.Scanner;

public class Java71 {

public static void main(String[] args) {
// TODO code application logic here
Scanner s = new Scanner(System.in);
System.out.println("请输入字符串:");
String a = s.nextLine();
int abccount = 0;
int numcount = 0;
int spacecount = 0;
int othercount = 0;
char[] b = a.toCharArray();
for(int i = 0; i < b.length; i++){
if(b[i]>='a'&&b[i]<='z'||b[i]>='A'&&b[i]<='Z'){
abccount++;
}else if(b[i]>='0'&&b[i]<='9'){
numcount++;
}else if(b[i]==' '){
spacecount++;
}else{
othercount++;
}
}
System.out.println("字符串中含有的英文字母数为:" + abccount);
System.out.println("字符串中含有的数字数为:" + numcount);
System.out.println("字符串中含有的空格数为:" + spacecount);
System.out.println("字符串中含有的其他字符为:" + othercount);
}
}
温馨提示:答案为网友推荐,仅供参考
相似回答