用Java来表示
package lianxi;
import java.util.Scanner;
public class Geshu {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int num=0;
int c=0;
int kong=0;
int others=0;
//输入字符串
System.out.println("输入字符串");
String str=scan.nextLine();
/*String str=scan.next();
* String str=scan.nextline();
* 区别:
* next遇到空白制表回车字符结束(就是遇到空格结束)
* nextline遇到换行符结束(运行程序的最后一下回车)
*/
for(int i=0;i<str.length();i++) {
//字符串一个一个判断字符
char ch=str.charAt(i);
if(ch>='0'&&ch<='9') {
num++;
}
else if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z') {
c++;
}
else if(ch==' ') {
kong++;
}
else {
others++;
}
}
System.out.println("数字个数:"+num);
System.out.println("字符个数:"+c);
System.out.println("空格个数:"+kong);
System.out.println("其他字符:"+others);
}
}
1,正常输入是三个数字,三个字符,三个空格,还有第三个其他符号,(代码在上面)
现在做一个改动把String str=scan.nextline();改成String str=scan.next();
之后只要遇到字符串中有空格就会终止,因此无法用它来扫描空格的字数,会直接中断