package October;
import java.util.Scanner;
public class Common {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//因为scanner.next的默认机制是遇到空格和换行符就会停下
// 加上这行代码改变它的匹配方式,使得它可以接收空格
sc.useDelimiter("\n");
String ch = sc.next();
System.out.println(ch);
judge(ch);
}
public static void judge(String ch) {
if (ch == null){
throw new RuntimeException("不能为空");
}
int num=0,letter1=0,letter2=0,other=0;
for(int i = 0; i <ch.length();i++){
if(ch.charAt(i) >= '0'&&ch.charAt(i) <= '9'){
num++;
}else if(ch.charAt(i) >= 'a'&&ch.charAt(i) <= 'z'){
letter1++;
}else if(ch.charAt(i) >= 'A'&&ch.charAt(i)<='Z'){
letter2++;
}else {
other++;
}
}
System.out.println("大写字母有"+letter1+"个"+"\n"+"小写字母有"+letter2+"个"+"\n"+"数字有"+num+"个"+"\n"+"其他字符有"+other+"个");
}
}
输入一串字符,判断大小写字母和数字各几个
最新推荐文章于 2022-01-26 09:20:43 发布