package day2;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
// int word,space,num,other=0;
System.out.println("请输入一行字符");
Scanner scan=new Scanner(System.in);
String str;
char c;
int word=0,space=0,number=0,other=0;
str=scan.nextLine();
for(int i=0;i<str.length();i++) {
c=str.charAt(i);
if(c>='a'&&c<='z'||c>='A'&&c<='Z') {
word++;
}else if(c==' ') {
space++;
}else if(c>='0'&&c<='9') {
number++;
}else {
other++;
}
}
System.out.println("英文字母有:"+word+" 空格有:"+space+" 数字有:"+number+" 其他字符有:"+other);
}
}
输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
返回指定位置的字符
判断字符的所属范围
区别scan.nextLine ()与scan.next()
用 Scanner 实现字符串的输入有两种方法,一种是next(),一种nextLine();
next() 一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键、Tab键或Enter键等结束符,next() 方法会自动将其去掉,只有在输入有效字符之后,next()方法才将其后输入的空格键、Tab键或Enter键等视为分隔符或结束符。
nextLine()方法的结束符只是Enter键。
简言之,next方法不能得到带空格的字符串,而nextLine()方法返回的是Enter键之前的所有字符,
————————————————
版权声明:本文为CSDN博主「BugFree_张瑞」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u011489043/article/details/65644104