/*输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数*/
import java.util.*;
public class StringManipulation {
public static void main(String[] args) {
// TODO Auto-generated method stub
int letter=0;//英文字母
int spaces=0;//空格
int numbers=0;//数字
int others = 0;//其他
String str;//待输入字符串
Scanner sc=new Scanner(System.in);
str=sc.nextLine();//读取字符串到str
int string_length = str.length();//字符串长度
char[] arr = str.toCharArray();
for (int i = 0;i < string_length;i++)//循环判断字符串每一位
{
if ((arr[i] >= 'a'&&arr[i] <= 'z') || (arr[i] >= 'A'&&arr[i] <= 'Z'))
{
letter++;
}
else if (arr[i] >= '0' && arr[i] <= '9')
{
numbers++;
}
else if (arr[i] == ' ')
{
spaces++;
}else
{
others++;
}
}
System.out.print("字母:"+letter+"\n"+"数字:"+numbers+"\n"+"空格:"+spaces+"\n"+"其他:"+others+"\n");
sc.close();
}
}
JAVA经典50题(7)
最新推荐文章于 2022-12-07 16:46:07 发布
308

被折叠的 条评论
为什么被折叠?



