-
题目:输入一行字符,分别统计出其英文字母、空格、数字和其它字符的个数
1、获取一行字符串,nextLine()2、把字符串的每一个字符赋值到一个数值中
3、对比每一个数值在ASK码的范围,就可以确定它符号的类别
4、char字符ASK码的范围
(1)数字0到9: 48~57
(2)字母A到Z:65到90
a到z:97到122
(3)空格是32
package suanfa;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一串字符串:");
String zi=sc.nextLine();
//先定义变量统计类型
int english=0;//65-90,97-122
int kong=0;//32
int math=0;//48-57
int others=0;
//关键在于把string字符串转为char数组,然后拿char字符ASK码来进行操作
char[] fen_jie=zi.toCharArray();
for (int i = 0; i <fen_jie.length; i++) {
if(fen_jie[i]>=65&&fen_jie[i]<=90||fen_jie[i]>=97&&fen_jie[i]<=122){
english++;
}else if(fen_jie[i]==32){
kong++;
}else if(fen_jie[i]>=48&&fen_jie[i]<=57){
math++;
}else{
others++;
}
}
System.out.println("字母有"+english+"个");
System.out.println("空格有"+kong+"个");
System.out.println("数字有"+math+"个");
System.out.println("其他字符有"+others+"个");
}
}