分析以下需求,并用代码实现:
(1)从键盘录入一个字符串
(2)统计该串中有大写字母、小写字母、数字各有多少个。
举例:
Hello12345World
大写 : 2个
小写 : 8个
数字 : 5个
package com.heima.test;
import java.util.Scanner;
public class Day12_Work2 {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int big = 0;
int small = 0;
int num = 0;
int other = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c>='A'&&c<='Z'){
big++;
}else if(c>='a'&&c<='z'){
small++;
}else if(c>='1'&&c<='9'){
num++;
}else{
other++;
}
}
System.out.println("大写:"+big+"个");
System.out.println("小写:"+small+"个");
System.out.println("数字:"+num+"个");
}
}
|
|