【问题描述】
输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。并打印出来。
【输入形式】
Please input a sentence:
【输出形式】
The number of numbers is:
The number of English letters is:
The number of blank space is:
The number of other characters is:
【输入样例】
This is my 1st JAVA code!
【输出样例】
The number of numbers is: 1
The number of English letters is: 18
The number of blank space is: 5
The number of other characters is: 1
【评分标准】
结果完全正确得20,每个测试点4分。
代码块
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] a = new int[26];
System.out.println("Please input a sentence:");
String str = sc.nextLine();
str = str.trim().toLowerCase();
int x=0,y=0,z=0,w=0;
for (int i = 0; i < str.length(); i++) {
char c=str.charAt(i);
if((c >= 'a' && c <= 'z')||(c == ' ')||(c >= '0' && c <= '9')) {
if (c >= 'a' && c <= 'z') {
x++;
}
if (c==' ') {
y++;
}
if (c >= '0' && c <= '9') {
z++;
}
}
else{
w++;
}
}
System.out.println("The number of numbers is: " + z);
System.out.println("The number of English letters is: " + x);
System.out.println("The number of blank space is: " + y);
System.out.println("The number of other characters is: " + w);
}
}