@Test
public void test5s(){
// {'a','3',' '}
//"abC34 3&%$# ^$qw12*-"
// String str = "abC34 3&%$# ^$qw12*-";
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要统计的字符串");
String str = scanner.nextLine();
char[] array = str.toCharArray();
int countLetter = 0;
int countSpace = 0;
int countNumb = 0;
int countOther = 0;
for (int i = 0; i < array.length ; i++) {
if ((array[i] >= 'a' && array[i] <='z')
|| (array[i] >= 'A' && array[i] <= 'Z')){
countLetter++;
} else if (array[i] == ' '){
countSpace++;
} else if (array[i] >= '0' && array[i] <= '9'){
countNumb++;
} else {
countOther++;
}
}
System.out.println("字母的个数: " + countLetter);
System.out.println("空格的个数: " + countSpace);
System.out.println("数字的个数: " + countNumb);
System.out.println("其他的个数: " + countOther);
}
输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
最新推荐文章于 2022-12-09 09:53:32 发布