题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数.
class Note{
public class void main(String[] args){
Scanner input = new Scanner(System.in);
int alphabeticCount = 0;
int spaceCount = 0;
int digitCount = 0;
int otherCount = 0;
System.out.println("请输入:");
String str = input.nextLine();
char[] ch = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
if (Character.isAlphabetic(ch[i])) {
alphabeticCount++;
} else if (Character.isSpaceChar(ch[i])) {
spaceCount++;
} else if (Character.isDigit(ch[i])) {
digitCount++;
} else {
otherCount++;
}
}
System.out.println("数字的个数:"+digitCount++);
System.out.println("字母的个数:"+alphabeticCount++);
System.out.println("空格的个数:"+spaceCount++);
System.out.println("其它的个数:"+otherCount++);
Scanner inputTwo =new Scanner(System.in);
System.out.println("请输入数字:");
int digit = input.nextInt();
System.out.println("请输入相加几次:");
int digitThree = input.nextInt();//几个数相加有键盘控制
int sum=0;
int sumTotal=0;
for (int i = 0; i
The results are as follows:
请输入:
今天天气真不错啊! The weather is really nice today!
数字的个数:0
字母的个数:35
空格的个数:6
其它的个数:2
请输入数字:
5
请输入相加几次:
5
61725
Process finished with exit code 0