统计–键盘输入的字符串中各种字符的个数
package ShangXueTang;
//自动导包:sc+alt+/
import java.util.Scanner;
/*题目:统计--键盘输入的字符串中各种字符的个数
*/
public class Test_02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请键盘输入字符串:");
String input = sc.next();
int countdaxie = 0; // 累加 大写字母的
int countxiaoxie = 0; // 小写字母
int countshuzi = 0; // 数字
int countother = 0; // 其它
// 将键盘输入的字符串 转换成 字符数组
char[] charArray = input.toCharArray();
// 遍历数组 获取 每个字符
for (int i = 0; i < charArray.length; i++) {
//获取到每一个字符
char dange = charArray[i];
//判断
if ('A' <= dange && dange <= 'Z') {
countdaxie++;
} else if ('a' <= dange && dange <= 'z') {
countxiaoxie++;
} else if ('0' <= dange && dange <= '9') {
countshuzi++;
} else {
countother++;
}
}
System.out.println("大写字母总个数:"+countdaxie);
System.out.println("小写字母总个数:"+countxiaoxie);
System.out.println("数字总个数: "+countshuzi);
System.out.println("其它字符总个数:"+countother);
}
}
