public class Main {
public static void main(String args[]){
String str1="abfdTE1879!!";//可以从控制台输入
String str2=str1.replaceAll("[a-z|A-Z]","");
System.out.println("英文字符的个数为"+(str1.length()-str2.length()));
str1=str2;
str2=str1.replaceAll("[0-9]","");
System.out.println("数字字符的个数为"+(str1.length()-str2.length()));
System.out.println("其它字符的个数为"+str2.length());
}
}
这是最简洁的
运行示例:
英文字符的个数为6
数字字符的个数为4
其它字符的个数为2
public static void main(String[] args) {
int count_abc=0,count_num=0,count_oth=0;
//输入一串数
Scanner scan=new Scanner(System.in);
String str = scan.next();
char[] chars = str.toCharArray();
//判断每个字符
for(int i = 0;i
count_abc++;
}else if(chars[i]>=48&&chars[i]<=57){
count_num++;
}else{
count_oth++;
}
}
System.out.println("字母有:"+count_abc+"个");
System.out.println("数字有:"+count_num+"个");
System.out.println("其他的有:"+count_oth+"个");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int abcCount=0;//英文字母个数
int spaceCount=0;//空格键个数
int numCount=0;//数字个数
int otherCount=0;//其他字符个数
Scanner scan=new Scanner(System.in);
String str=scan.nextLine();
char[] ch = str.toCharArray();
for(int i=0;i
//判断是否字母
abcCount++;
}
else if(Character.isDigit(ch[i])){
//判断是否数字
numCount++;
}
else if(Character.isSpaceChar(ch[i])){
//判断是否空格键
spaceCount++;
}
else{
//以上都不是则认为是其他字符
otherCount++;
}
}
System.out.println("字母个数:"+abcCount);
System.out.println("数字个数:"+numCount);
System.out.println("空格个数:"+spaceCount);
System.out.println("其他字符个数:"+otherCount);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
String getMess=scan.nextLine();
int zm=0;
int num=0;
int other=0;
char [] ch = getMess.toCharArray();
for(int i=0;i
zm=zm+1;
}else if(ch[i]>47 && ch[i]<58){
num=num+1;
}else{
other=other+1;
}
}
System.out.println("字母:"+zm);
System.out.println("数字:"+num);
System.out.println("其它:"+other);
}