public Map<String,Integer> getCharacterNum(String str)
{
//封装一个Map,key为String类型,value为字母数,其中key为UpperChar的value存大写字母 数
// key 为NumberChar的value存放数字
//key为LowerChar的value存放小写字母数,key为OtherChar的value存放非英文字母数
Map<String,Integer> map = new HashMap<String,Integer>();
int upperValue = 0;
int lowerValue = 0;
int otherValue = 0;
int numberValue = 0;
for (int i = 0 ; i < str.length(); i++)
{
if (str.charAt[i] > 'a' && str.charAt[i] < 'z')
{
lowerValue++;
}
else if (str.charAt[i] > 'A' && str.charAt[i] < 'Z')
{
upperValue++;
}
else if (str.charAt[i] > '0' && str.charAt[i] < '9')
{
numberValue ++;
}
else
{
otherValue++;
}
}
map.put("UpperChar",upperValue);
map.put("LowerChar",lowerValue);
map.put("OtherChar",otherValue);
map.put("NumberChar",numberValue);
return map;
}
public static void main(Stirng[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String str = sc.nextLine();
类名 objClass = new 类名(); //这里的类名为当前创建的类名
Map<String,Integer> dataMap = objClass.getCharacterNum(str);
//大写字母的数量
int upChar = dataMap.get("UpperChar");
//小写字母数量
int lowerChar = dataMap.get("LowerChar");
//数字数量
int numChar = dataMap.get("NumberChar");
//其他字母数量
int otherChar = dataMap.get("OtherChar");
}
温馨提示:答案为网友推荐,仅供参考