用Java编写一个程序

用Java编写一个程序,输出一个字符串中的大写英文字母数,小写英文字母数,数字的个数,其他字符的个数。
要求,从键盘上获得字符串。
用import java.io.*;
import java.util.Scanner;……

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");
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-04-01
public static void test_7(){
System.out.println("请输入一个字符串:");

String str = in.nextLine();
char[] arr = str.toCharArray();
int a=0;
int b=0;
int c=0;
int d=0;
for(int i=0;i<arr.length;i++){
if(arr[i]>='A' && arr[i]<='Z')
a++;
else if(arr[i]>='a' && arr[i]<='z')
b++
else if(arr[i]>='0' && arr[i]<='9')
c++;
else
d++;
}
System.out.print("大写字母个数为:"+a+"\n小写字母个数为:"+b+"\n数字的个数为:"+c+"\n其他字符个数为:"+d);
}
第2个回答  2014-04-01
import java.util.Scanner;

public class Test4_9 {
public static void main(String[] args) {
String str = "";
System.out.println("请输入字符串:");
Scanner console = new Scanner(System.in);
str = console.nextLine();
byte[] arr = str.getBytes();
long dx=0,xx=0,zs=0,qt=0;
for (int i = 0; i < arr.length; i++) {
if(arr[i]>='a' && arr[i]<='z'){
dx++;
}else if(arr[i]>='A' && arr[i]<='Z'){
xx++;
}else if(arr[i]>='0' && arr[i]<='9'){
zs++;
}else{
qt++;
}
}
System.out.println("大写字母:"+dx+"\t小写字母:"+xx+"\t整数:"+zs+"\t其他:"+qt);
}
}本回答被提问者和网友采纳