举例:
Hello12345World
大写 : 2个
小写 : 8个
数字 : 5个
package com.heima.work;
import java.util.Scanner;
public class Demo4_Work {
public static void main(String[] args) {
System.out.println("请输入一个字符串");
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
getNum(line);
}
public static void getNum(String line) {
int big = 0;
int small = 0;
int num = 0;
int other = 0;
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if(c >= 'A' && c <= 'Z' ) {
big++;
}else if(c >= 'a' && c <= 'z') {
small++;
}else if(c >= '0' && c <= '9') {
num++;
}else {
other++;
}
}
System.out.println("大写:" + big + "个");
System.out.println("小写:" + small + "个");
System.out.println("数字:" + num + "个");
System.out.println("其它:" + other + "个");
}
}
|
|