输入一段字符串,其中包含字母数字符号,分别统计他们的个数,用java代码书写
时间: 2024-05-14 19:11:49 浏览: 44
好的,以下是Java代码,实现统计字符串中字母、数字、符号个数的功能:
```
import java.util.Scanner;
public class CountChar {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
int letterCount = 0;
int digitCount = 0;
int symbolCount = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isLetter(c)) {
letterCount++;
} else if (Character.isDigit(c)) {
digitCount++;
} else {
symbolCount++;
}
}
System.out.println("字母个数:" + letterCount);
System.out.println("数字个数:" + digitCount);
System.out.println("符号个数:" + symbolCount);
}
}
```
阅读全文