import java.util.ArrayList;
import java.util.Scanner;
public class Question07 {
public static void main(String[] args) {
System.out.println("请输入英文字符串:");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
ArrayList<Integer> al = getNumber(input);
if(al.size() == 0) {
System.out.println("输入的字符串长度为0");
}else {
System.out.printf("数字的个数未%d,英文的个数为%d,空格有%d,其他字符个数为%d",
al.get(1),al.get(0),al.get(2),al.get(3));
}
}
public static ArrayList<Integer> getNumber(String input) {
ArrayList<Integer> al = new ArrayList<Integer>();
int numbersOfFigure = 0;
int numberOfChinese = 0;
int numberOfEnglish = 0;
int numberOfEmpty = 0;
while(input.length() == 0) {
return al;
}
String[] strings = input.split("");
for (int i = 0; i < strings.length; i++) {
if(strings[i].matches("[0-9]")) {
numbersOfFigure++;
}else if(strings[i].matches("[A-Za-z]")) {
numberOfEnglish++;
}else if(strings[i].matches(" ")){
numberOfEmpty++;
}else {
numberOfChinese++;
}
}
al.add(numberOfEnglish);
al.add(numbersOfFigure);
al.add(numberOfEmpty);
al.add(numberOfChinese);
return al;
}
}
05-01
05-01
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交