题目:
让用户输入一个字符串,我们来统计这个字符串中字母大小写和数字以及其他东西的个数,并输出。
package mmm.jsu.oop;
import java.util.Scanner;
public class DemoCode02 {
public static void main(String[] args) {
Scanner str = new Scanner(System.in);
//输入一个字符串
String code = str.next();
//定义3个计数器
int Bigcount = 0;
int Smallcount = 0;
int Count = 0;
int Other = 0;
//遍历字符串并进行判断和计数
for (int i = 0; i < code.length(); i++) {
char sm = code.charAt(i);
if(sm >= 'A' && sm <= 'Z'){
Bigcount++;
} else if (sm >= 'a' && sm <= 'z') {
Smallcount++;
} else if (sm >= '0' && sm <= '9') {
Count++;
}else{
Other++;
}
}
System.out.println("这个字符串里面有大写字母的" + Bigcount + "个");
System.out.println("这个字符串里面有小写字母的" + Smallcount + "个");
System.out.println("这个字符串里面有数字的" + Count + "个");
System.out.println("这个字符串里面其他的" + Other + "个");
}
}